模仿服务器端与客户端之间的通信:
服务器端实现多进程返回客户端的输入信息的功能。
客户端实现数据的写入传输了打印。
服务器端:
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m)\ //宏定义
do \
{\
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
void do_service(int conn) //返回客户端数据
{
char recvbuff[1024];
while(1)
{
memset(recvbuff, 0, sizeof(recvbuff));
int ret = read(conn, recvbuff, sizeof(recvbuff));
if(ret == 0)
{
printf("client_close\n");
break;
}
else if(ret == -1)
ERR_EXIT("read");
fputs(recvbuff, stdout);
write(conn, recvbuff, ret);
}
}
int main(void)
{
int listenfd;
if((listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) //创建一个socket
/*if((listenfd = socket(AF_INET, SOCKET_STREAM, 0)) < 0)*/
ERR_EXIT("socket");
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr)); //初始化sockaddr_in(IP地址及端口)
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");*/
/*inet_aton("127.0.0.1", &servaddr.sin_addr);*/
int on = 1;
if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)//使用REUSEADDR使得端口立即关闭
ERR_EXIT("setsockopt");
if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) //绑定socket
ERR_EXIT("bind");
if(listen(listenfd, SOMAXCONN) < 0) //监听socket,程序阻塞等待连接
ERR_EXIT("listen");
struct sockaddr_in perraddr; //初始化连接的socket
socklen_t peerlen = sizeof(perraddr);
int conn;
pid_t pid;//使用fork()创建子进程,函数返回两个参数,子进程pid = 0,父进程是pid = 子进程pid号
while(1)
{
if((conn = accept(listenfd, (struct sockaddr*)&perraddr, &peerlen)) < 0)<span style="font-family:KaiTi_GB2312;">//进行TCP三次握手</span>
ERR_EXIT("accept");
printf("ip=%s port=%d\n", inet_ntoa(perraddr.sin_addr), ntohs(perraddr.sin_port));
pid = fork();
if(pid == -1)
ERR_EXIT("fork");
if(pid == 0)
{
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
}
else
close(conn);
}
return 0;
}
客户端:
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m)\
do \
{\
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(void)
{
int sock;
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) //初始化socket
/*if((listenfd = socket(AF_INET, SOCKET_STREAM, 0)) < 0)*/
ERR_EXIT("socket");
struct sockaddr_in servaddr; //初始化sockaddr_in
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) //连接客户端,进行TCP三次握手
ERR_EXIT("connect");
char sendbuff[1024] = {0}; //初始化发送字符数组
char recvbuff[1024] = {0}; //初始化接受字符数组
while(fgets(sendbuff, sizeof(sendbuff), stdin) != NULL)
{
write(sock, sendbuff, strlen(sendbuff));
read(sock, recvbuff, sizeof(recvbuff));
fputs(recvbuff, stdout);
memset(&recvbuff, 0, sizeof(recvbuff));
memset(&sendbuff, 0, sizeof(sendbuff));
}
close(sock);
return 0;
}