初学Linux Socket网络编程,在编译后执行server端文件时,server端accept在等待,client端connect失败。
server端代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SOCKET_PORT 8080
int main()
{
//创建服务端socket
int serverfd;
if((serverfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
printf("socket create error\n");
return -1;
}
else
{
printf("socket create OK! \n");
}
//把服务端用于通信的地址和端口绑定到socket上
struct sockaddr_in servaddr; //服务端地址信息的数据结构
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET; //协议族,在socket编程中只能是AF_INET
servaddr.sin_addr.s_addr = inet_addr("192.168.157.129"); //IP
servaddr.sin_port = htonl(SOCKET_PORT); //指定通信端口
if(-1 == bind(serverfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))
{
printf("bind error\n");
close(serverfd);
return -1;
}
else
{
printf("bind OK! \n");
}
//把socket设置为监听模式
if (-1 == listen(serverfd,5))
{
printf("listen error\n");
close(serverfd);
return -1;
}
else
{
printf("listen stauts OK! \n");
}
//接受客户端的连接
struct sockaddr_in clientaddr; //客户端的地址信息
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htonl(SOCKET_PORT);
clientaddr.sin_addr.s_addr = inet_addr("192.168.157.129");
int socklen = sizeof(clientaddr); //struct sockaddr_in的大小
printf("accept stauts before! \n");
int clientfd = accept(serverfd,(struct sockaddr *)&clientaddr,&socklen);
if(-1 == clientfd)
{
printf("accept error\n");
close(serverfd);
return -1;
}
else
{
printf("客户端(%s)已连接 \n",inet_ntoa(clientaddr.sin_addr));
}
//与客户端通信,接收客户端发过来的报文后,回复OK
char buffer[1024];
while (1)
{
int ret;
memset(buffer,0,sizeof(buffer));
if((ret = recv(clientfd,buffer,sizeof(buffer),0)) <= 0) //接收客户端的请求报文
{
printf("ret = %d \n",ret);
break;
}
printf("接收: %s \n",buffer);
strcpy(buffer,"recv client req OK");
if((ret = send(clientfd,buffer,strlen(buffer),0)) <=0) //向客户端发送应答报文
{
perror("send resp error");
break;
}
printf("发送: %s \n",buffer);
}
}
client端代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SOCKET_PORT 8080
int main()
{
int ret = 0;
//创建客户端socket
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockfd)
{
printf("socket error \n");
return -1;
}
else
{
printf("socket创建成功! \n");
}
//初始化端口和服务端地址
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htonl(SOCKET_PORT); //指定服务器的端口
servaddr.sin_addr.s_addr = inet_addr("192.168.157.129");
//向服务器端发起连接请求
ret = connect(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr));
if( -1 == ret)
{
printf("connect error \n");
close(sockfd);
return -1;
}
else
{
printf("与服务端连接成功! \n");
}
//与服务端通信,发送一个报文后等待回复,然后再发下一个报文
char buffer[1024];
int data_cnt = 0;
for (data_cnt; data_cnt < 10; data_cnt++)
{
memset(buffer,0,sizeof(buffer));
sprintf(buffer,"创建第%d个socket通信,编号%03d \n",data_cnt+1,data_cnt+1);
if( (ret = send(sockfd,buffer,strlen(buffer),0)) <=0 ) //向服务端发送请求报文
{
printf("send req error \n");
break;
}
else
{
printf("发送:%s \n",buffer);
}
if( (ret = recv(sockfd,buffer,sizeof(buffer),0)) <=0 ) //接收服务端的应答报文
{
printf("ret = %d \n",ret);
break;
}
else
{
printf("接收: %s \n",buffer);
}
}
printf("socket通信完成!");
close(sockfd);
}
server端执行后信息:
[misaka@192 socket]$ ./socket_test_server
socket create OK!
bind OK!
listen stauts OK!
accept stauts before!
client端执行后信息:
[misaka@192 socket]$ ./socket_test_client
socket创建成功!
connect error
环境:centos,VS CODE