#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;
}
服务端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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
#include /* See NOTES */
#include
#include
#include /* superset of previous */
#include
#include
#include
#include
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;
}