TCP/IP回声服务器客户端

时间:2022-02-16 10:19:43

基于上次的helloworld改写成回声服务器/客户端。
server.c

for(i=0;i<5;i++)
{
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_adr,&clnt_adr_sz);
if(clnt_sock == -1)
error_handling("accept error");
else
printf("connect client %d \n",i+1);
while((str_len == read(clnt_sock,message,BUF_SIZE)) !== 0)
write(clnt_scok,message,str_len);
close(clnt_sock);
}
==========================================================================
client.c

while(1)
{
fputs(“Inputs message(Q to quit): “,stdout);
fgets(message,BUF_SIZE,stdin);
if(!strcmp(message,”Q\n”) || !strcmp(message,”q\n”))
break;
write(scok,message,strlen(message));
str_len = read(sock,message,BUF_SZIE-1);
printf(“Message from server : %s”,message);
}
“`