1. tcp 客户端、服务端通讯
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>#define SERVICE_PORT 9994
#define SERVICE_IP "192.168.0.106"int main(int argc, const char *argv[])
{//服务端//1. 创建socketint serviceId;if((serviceId = socket(AF_INET,SOCK_STREAM,0)) == -1){perror("service socket error");return -1;}//2. 绑定struct sockaddr_in serviceAddr;serviceAddr.sin_family = AF_INET;serviceAddr.sin_port = htons(SERVICE_PORT);serviceAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);if(bind(serviceId,(struct sockaddr*)&serviceAddr,sizeof(serviceAddr)) == -1){perror("service bind error");return -1;}//3.监听if(listen(serviceId,128) ==-1){perror("service listen error");return -1;}printf("service success %d\n",serviceId);//4. 接收连接请求,接收客户端ipint newServiceId;struct sockaddr_in cliAddr;int cliAddrLen = sizeof(cliAddr);if((newServiceId = accept(serviceId,(struct sockaddr*)&cliAddr,&cliAddrLen)) == -1){perror("service accept error");return -1;}printf("[%s:%d]\n",inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port));//5. 循环接收消息char sbuf[120] ="";while(1){bzero(sbuf,sizeof(sbuf));//读取int len = recv(newServiceId,sbuf,sizeof(sbuf),0);printf("[%s:%d ] 客户端发送的消息 %s \n",inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),sbuf);if(len == 0){printf("client break\n");break;}if(strcmp(sbuf,"tc") ==0){printf("service break\n");break;}//拼接个笑脸发送给客户端strcat(sbuf,"*_*");send(newServiceId,sbuf,sizeof(sbuf),0);}//关闭文件描述符close(newServiceId);close(serviceId);return 0;
}#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>#define SERVICE_PORT 9994
#define SERVICE_IP "192.168.0.106"int main(int argc, const char *argv[])
{//客户端//1.创建socketint clientId;if((clientId = socket(AF_INET,SOCK_STREAM,0)) == -1){perror("client socket error");return -1;}//2. 客户端不绑定//3. 客户端连接服务器端struct sockaddr_in cliAddr;cliAddr.sin_family = AF_INET;cliAddr.sin_port = htons(SERVICE_PORT);cliAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);if(connect(clientId,(struct sockaddr*)&cliAddr,sizeof(cliAddr)) == -1){perror("client connect error");return -1;}printf("client socket success\n");//4.发送消息给客户端及接收服务器端消息char cbuf[120] ="";while(1){bzero(cbuf,sizeof(cbuf));fgets(cbuf,sizeof(cbuf),stdin);//处理\ncbuf[strlen(cbuf) -1] = 0;//发送消息send(clientId,cbuf,sizeof(cbuf),0);bzero(cbuf,sizeof(cbuf));//接收到的消息int len = recv(clientId,cbuf,sizeof(cbuf),0);printf("[%s:%d] 服务器发送的消息 %s\n",SERVICE_IP,SERVICE_PORT,cbuf);if(len == 0 ){printf("service break\n");break;}if(strcmp(cbuf,"tc") == 0){printf("client break");break;}}//关闭socket流描述符close(clientId);return 0;
}
2.udp 客户端 服务器端通讯
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>#define SERVICE_PORT 8889
#define SERVICE_IP "192.168.0.106"int main(int argc, const char *argv[])
{//udp服务端//1.创建socketint udpService;if((udpService = socket(AF_INET,SOCK_DGRAM,0)) == -1){perror("udpService socket error");return -1;}//绑定struct sockaddr_in udpAddr;udpAddr.sin_family = AF_INET;udpAddr.sin_port = htons(SERVICE_PORT);//点分十进制 ip 转化成 网络套接字 udpAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);if(bind(udpService,(struct sockaddr*)&udpAddr,sizeof(udpAddr)) == -1){perror("udpService bind error");return -1;}//服务端获取到客户端 ip 及端口struct sockaddr_in udpCliAddr;int udpCliAddrLen = sizeof(udpCliAddr);printf("udpService success\n");//接收/发送消息char ubuf[120] ="";while(1){bzero(ubuf,sizeof(ubuf));int len = recvfrom(udpService,ubuf,sizeof(ubuf),0,(struct sockaddr*)&udpCliAddr,&udpCliAddrLen);printf("[%s:%d] 客户端发送的消息 %s\n",inet_ntoa(udpCliAddr.sin_addr),ntohs(udpCliAddr.sin_port),ubuf);if(strcmp(ubuf,"tc") == 0){printf("udpService break\n");break;}//发送strcat(ubuf,"*_*");sendto(udpService,ubuf,sizeof(ubuf),0,(struct sockaddr*)&udpCliAddr,udpCliAddrLen);}close(udpService);return 0;
}#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>#define SERVICE_PORT 8889
#define SERVICE_IP "192.168.0.106"int main(int argc, const char *argv[])
{//udp客户端//1. 创建socketint udpClient;if((udpClient = socket(AF_INET,SOCK_DGRAM,0)) == -1){perror("udpClient socket error");return -1;}struct sockaddr_in udpService;udpService.sin_family = AF_INET;udpService.sin_port = htons(SERVICE_PORT);udpService.sin_addr.s_addr = inet_addr(SERVICE_IP);//int udpLen = sizeof(udpService);printf("udpClient success\n");char ucbuf[120] ="";while(1){bzero(ucbuf,sizeof(ucbuf));printf("发送消息 ");fgets(ucbuf,sizeof(ucbuf),stdin);//处理\nucbuf[strlen(ucbuf)-1] = 0;//发送sendto(udpClient,ucbuf,sizeof(ucbuf),0,(struct sockaddr*)&udpService,sizeof(udpService));bzero(ucbuf,sizeof(ucbuf));//接收int len = recvfrom(udpClient,ucbuf,sizeof(ucbuf),0,NULL,NULL);printf("[%s:%d] 服务端回复的消息 %s\n",SERVICE_IP,SERVICE_PORT,ucbuf);if(strcmp(ucbuf,"tc") == 0){printf("udpClient break\n");break;}}close(udpClient);return 0;
}
3. 思维导图