一、服务器端
//
TCP 通信的服务器
//
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<arpa/inet.h>
#include <string.h>
int main(){
// 1. 创建socket(用于监听的套接字)
int lfd = socket(AF_INET, SOCK_STREAM,0);
if(lfd==-1){
perror("socket");
exit(-1);
}
// 2. 绑定bind 服务器的ip和端口
// int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
// const struct sockaddr *addr 使用通用的的sockaddr_in
struct sockaddr_in saddr; // socket地址
saddr.sin_family = AF_INET; // 协议族
// inet_pton(AF_INET,"192.168.4.90",&saddr.sin_addr.s_addr);
// 这里的INADDR_ANY 是0
// 由于本机不同的MAC可能有多个ip,0代表哪一个都行
saddr.sin_addr.s_addr= INADDR_ANY;
saddr.sin_port = htons(9999);
int ret1 = bind(lfd,(struct sockaddr *)&saddr,sizeof(saddr));
if(ret1==-1){
perror("bind");
exit(-1);
}
// 3. 监听
int ret2 = listen(lfd,8);
if(ret2 == -1){
perror("listen");
exit(-1);
}
//4. 接受客户端连接
struct sockaddr_in clientaddr;
int len =sizeof (clientaddr);
int cfd = accept(lfd,(struct sockaddr * )&clientaddr,&len );
if(cfd == -1){
perror("accept");
exit(-1);
}
// 输出客户端信息
char clientIP[16];
inet_ntop(AF_INET,&clientaddr.sin_addr.s_addr,clientIP,sizeof(clientIP));
unsigned short clientPort = ntohs(clientaddr.sin_port);
printf("client ip is %s,port is %d\n",clientIP,clientPort);
// 5. 通信
// 获取客户端的数据
// char recvBuf[1024]={0};
// int len1 = read(cfd,recvBuf,sizeof(recvBuf));
// if(len1==-1){
// perror("read");
// exit(-1);
// }else if(len1 >0){
// printf("recv client data :%s\n",recvBuf);
// }else if(len1==0){
// // 表示客户端断开
// printf("client closed");
// }
// // 给客户端发送数据
// char* data = "hello,i am server";
// write(cfd,data, strlen(data));
char recvBuf[1024]={0};
while (1){
int len1 = read(cfd,recvBuf,sizeof(recvBuf));
if(len1==-1){
perror("read");
exit(-1);
}else if(len1 >0){
printf("recv client data :%s\n",recvBuf);
}else if(len1==0){
// 表示客户端断开
printf("client closed");
break;
}
// 给客户端发送数据
char* data = "hello,i am server";
write(cfd,data, strlen(data));
}
// 关闭文件描述符
close(lfd);// 关闭监听
close(cfd);// 关闭通信
return 0;
}
二、客户端
//
//
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<arpa/inet.h>
#include <string.h>
int main(){
// 1. 创建套接字
int fd = socket(AF_INET,SOCK_STREAM,0);
if(fd==-1){
perror("socket");
exit(-1);
}
// 2.连接服务器
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
inet_pton(AF_INET,"192.168.4.90",&serveraddr.sin_addr.s_addr);
serveraddr.sin_port = htons(9999);
int ret = connect(fd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if(ret==-1){
perror("connect");
exit(-1);
}
/* char * data = "hello,i am client";
write(fd,data, strlen(data));
int len1 = read(fd,recvBuf,sizeof(recvBuf));
if(len1==-1){
perror("read");
exit(-1);
}else if(len1 >0){
printf("recv server data :%s\n",recvBuf);
}else if(len1==0){
// 表示客户端断开
printf("server closed");
}*/
// 3.通信
char recvBuf[1024]={0};
while (1){
char * data = "hello,i am client";
write(fd,data, strlen(data));
int len1 = read(fd,recvBuf,sizeof(recvBuf));
if(len1==-1){
perror("read");
exit(-1);
}else if(len1 >0){
printf("recv server data :%s\n",recvBuf);
}else if(len1==0){
// 表示客户端断开
printf("server closed");
break;
}
}
close(fd);
return 0;
}