分析:首先接收函数需设计为线程,有连接请求后转给数据接收线程。客户端需改变bind的端口号。
设计下网络信息结构体,用于保存通信所用的套接字,tcpsocket
struct info{
int tcpsocket;
struct sockaddr_in client_info;
}
如果运行出现如下情况:
1.bind failed:Address already in use
原因是程序没有经过4次挥手,就中途退出来了,需等待一段时间或者kill掉此程序。
/********************************************************************
> File Name: ser1.c
> Author: xiening
> Mail: 1606598696@qq.com
> Created Time: 2020年09月30日 星期三 19时39分33秒
*******************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
/*
void handle(int signo)
{
close(sockfd);
close(tcp_sockfd);
printf("close sockfd tcp_sockfd\n");
exit(0);
}
*/
void *fuc(void *arg)
{
int tcp_sockfd =*((int*)arg);
char buf[1024];
//循环接收来信
while(1)
{
bzero(buf, 1024);
recv(tcp_sockfd, buf, 1024, 0);
if(!strcmp(buf, "88\n"))
{
printf("通信结束\n");
close(tcp_sockfd);
pthread_exit(NULL);
}
printf("recv:%s tid:%lx\n", buf, pthread_self());
}
}
int main(int argc , char const *argv[])
{
//1.打开网络文件并创建链接:
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
fprintf(stderr, "socket failed:%s\n", strerror(errno));
}
//定义结构体并且赋值:
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(6500);
myaddr.sin_addr.s_addr = inet_addr("0.0.0.0");//表示接受所有ip地址 INADDR_ANY
//绑定结构体和套接字
int ret = bind(sockfd, (struct sockaddr*)&myaddr,
sizeof(struct sockaddr_in));
if(ret == -1)
{
fprintf(stderr, "bind failed:%s\n", strerror(errno));
return -1;
}
else
{
printf("bind succeed\n");
}
//监听套接字:
ret = listen(sockfd, 5);//同时可以监听5个
if(ret == -1)
{
fprintf(stderr, "listen failed:%s\n", strerror(errno));
return -1;
}
else
{
printf("listen succeed\n");
}
struct sockaddr_in cliAddr;
//等待连接:
int len = sizeof(struct sockaddr);
while(1)
{
int tcp_sockfd = accept(sockfd, (struct sockaddr*)&cliAddr, &len);
if(tcp_sockfd == -1)
{
fprintf(stderr, "accept failed:%s\n", strerror(errno));
return -1;
}
else
{
printf("accept succeed\n");
}
pthread_t TID;
void *arg = (void*)&tcp_sockfd;
ret = pthread_create(&TID, NULL, fuc, arg);
if(ret != 0)
{
fprintf(stderr, "Created pthread failed:%s\n", strerror(errno));
}
}
close(sockfd);
return 0;
}
/********************************************************************
> File Name: ser.c
> Author: xiening
> Mail: 1606598696@qq.com
> Created Time: 2020年09月30日 星期三 19时39分33秒
*******************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc , char const *argv[])
{
//1.打开网络文件并创建链接:
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
fprintf(stderr, "socket failed:%s\n", strerror(errno));
return -1;
}
//定义结构体并且赋值:
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_port = htonl(1002);
myaddr.sin_addr.s_addr = inet_addr("0.0.0.0");//表示接受所有ip地址 INADDR_ANY
//绑定结构体和套接字
int ret = bind(sockfd, (struct sockaddr*)&myaddr,
sizeof(struct sockaddr_in));
if(ret == -1)
{
fprintf(stderr, "bind failed:%s\n", strerror(errno));
return -1;
}
//请求连接:
struct sockaddr_in srcAddr;
srcAddr.sin_family = AF_INET;
srcAddr.sin_port = htons(6500);
srcAddr.sin_addr.s_addr = inet_addr("192.168.137.50");
int len = sizeof(struct sockaddr);
ret = connect(sockfd, (struct sockaddr*)&srcAddr, len);
if(ret == -1)
{
fprintf(stderr, "connect failed:%s\n", strerror(errno));
return -1;
}
else
{
printf("connect succeed\n");
}
char buf[1024];
//循环接收来信
printf("发送消息:\n");
while(1)
{
bzero(buf, 1024);
fgets(buf, 1024, stdin);
ret = send(sockfd, buf, 1024, 0);
if(ret == -1)
{
printf("send failed\n");
return -1;
}
if(!strcmp(buf,"88\n"))
{
printf("通信结束\n");
break;
}
}
close(sockfd);
return 0;
}