1.服务器可以循环接收客户端的数据;
2.当客户端退出后, 服务器阻塞等待下一个客户端的连接,而后继续通信;
代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("sockfd is error:");
return -1;
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(8989);
saddr.sin_addr.s_addr = inet_addr("192.168.132.128");
if(bind(sockfd,(struct sockaddr *)&saddr,sizeof(saddr)) < 0)
{
perror("bind is error:");
return -1;
}
if(listen(sockfd,5) < 0)
{
perror("listen is error:");
return -1;
}
printf("Next is accept\n");
int acceptfd = accept(sockfd,NULL,NULL);
char stu[128];
while(1)
{
int res = recv(acceptfd,stu,sizeof(stu),0);
if(res == 0)
{
acceptfd = accept(sockfd,NULL,NULL);
res = recv(acceptfd,stu,sizeof(stu),0);
printf("client is exit:\n");
}
else if(res < 0)
{
perror("error:");
return -1;
}
else
{
printf("client is:%s\n",stu);
}
}
return 0;
}
实现结果:
3.当有客户端连接时, 服务器端 打印客户端的IP 和 Port信息;
代码如下:
运行结果:
4.将代码的 send 和 recv 改为 write 和 read, 测试一下效果;
5.利用argv[ ] 采用终端输入的方式, 输入IP 和 Port ,从而bind绑定;
整体代码:
服务端代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("sockfd is error:");
return -1;
}
struct sockaddr_in saddr,sty;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[1]));
saddr.sin_addr.s_addr = inet_addr(argv[2]);
if(bind(sockfd,(struct sockaddr *)&saddr,sizeof(saddr)) < 0)
{
perror("bind is error:");
return -1;
}
if(listen(sockfd,5) < 0)
{
perror("listen is error:");
return -1;
}
printf("Next is accept\n");
sty.sin_family = AF_INET;
int len = sizeof(sty);
int acceptfd = accept(sockfd,(struct sockaddr *)&sty,&len);
int a = ntohs(sty.sin_port);
printf("a = %d\n", a);
char* b = { inet_ntoa(sty.sin_addr)};
printf("b = %s\n", b);
char stu[128];
while(1)
{
int res = read(acceptfd,stu,sizeof(stu));
if(res == 0)
{
printf("client is exit:\n");
}
else if(res < 0)
{
perror("error:");
return -1;
}
else
{
printf("client is:%s\n",stu);
}
}
close(sockfd);
close(acceptfd);
return 0;
}
客户端代码:
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
//1. 创建套接字
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("socket is err:");
return -1;
}
struct sockaddr_in caddr;
caddr.sin_family = AF_INET;
//argv[1] 要写 服务器的Port
caddr.sin_port = htons(atoi(argv[1]));
//argv[2] 要写 服务器的IP
caddr.sin_addr.s_addr = inet_addr(argv[2]);
//2.主动连接 - 连接 caddr 的成员 - IP Port
if(connect(sockfd,(struct sockaddr *)&caddr,sizeof(caddr)) < 0)
{
perror("connect is err:");
return -1;
}
char buf[128];
//3.通信
while(1)
{
fgets(buf,sizeof(buf),stdin);
if(buf[strlen(buf)-1] == '\n');
buf[strlen(buf) -1] = '\0';
//发送
send(sockfd,buf,sizeof(buf),0);
}
close(sockfd);
return 0;
}