服务器端和客户端使用TCP的流程图:
服务器端:socket->bind->listen->accept->recv/recvfrom->send/sendto->close
客户端:socket->connect->send/sendto->recv/recvfrom->close
其中服务器端首先建立起socket,然后调用本地端口的绑定,接着就开始与客服端建立联系,并接收客户端发送的消息。客户端则在建立socket之后调用connect函数来建立连接。
服务器端的源代码如下所示:
/*"server.c"*/
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#define PORT 4321
#define BUFFER_SIZE 1024
#define MAX_QUE_CONN_NM 5
int main(){
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
int sockfd,client_fd;
char buf[BUFFER_SIZE];
if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){ //建立socket连接
perror("socket");
exit(1);
}
printf("Socket id=%d\n",sockfd);
/*设置sockaddr_in结构体中的相关参数*/
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(PORT);
server_sockaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
int i = 1; //允许重复使用本地地址与套接字进行绑定
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr)) == -1){ //绑定函数bind
perror("bind");
exit(1);
}
printf("Bind success!\n");
if(listen(sockfd,MAX_QUE_CONN_NM)== -1){ //调用listen函数,创建为处理请求的队列
perror("listen");
exit(1);
}
printf("Listening......\n");
if((client_fd = accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){//调用accept函数,等待客户端的接
perror("accept");
exit(1);
}
memset(buf,0,sizeof(buf));
if((recvbytes = recv(client_fd,buf,BUFFER_SIZE,0)) == -1){//调用recv函数接收客户端的请求
perror("recv");
exit(1);
}
printf("Received a message: %s\n",buf);
close(sockfd);
exit(0);
}