TCP服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>#define PORT 8888
#define IP "192.168.124.39"#define ERR_MSG(msg) do{\fprintf(stderr,"line:%d",__LINE__);\fprintf(stderr,"func:%s",__func__);\perror(msg);\}while(0)int main(int argc, const char *argv[])
{//创建流式套节奏int sfd=socket(AF_INET,SOCK_STREAM,0);if(sfd<0){ERR_MSG("socket");return -1;}printf("流式套节奏创建完毕 sfd=%d\n",sfd);//允许端口快速被复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速重用\n");//填充服务器地址信息结构体给bind函数使用//真实的地址信息结构体根据地址族制度,AF_INET-->>man 7 ipstruct sockaddr_in sin;sin.sin_family=AF_INET;//必须填AF_INETsin.sin_port=htons(PORT);//端口号的网络字节序 1024~49151sin.sin_addr.s_addr=inet_addr(IP);//本机IP,ifconfig,必须是桥接模式//绑定服务器地址信息,必须绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("bing");return -1;}//将套节奏设置为被动监听状态if(listen(sfd,128)<0){ERR_MSG("listen");return -1;}printf("监听成功\n");//获取连接成功的客户端信息,生成一个新的套节奏文件描述符struct sockaddr_in cin;socklen_t addrlen=sizeof(cin);int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);if(newfd<0){ERR_MSG("accpet");return -1;}printf("[%s:%d] newfd=%d 客户端链接成功\n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);char buf[128]="";ssize_t res;char buf2[128]="";ssize_t res2;while(1){bzero(buf,sizeof(buf));//接收res=recv(newfd,buf,sizeof(buf),0);if(res<0){ERR_MSG("recv");return -1;}else if(res==0){printf("[%s:%d]客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));break;}printf("[%s:%d]newfd=%d: %s\n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);//发送res2=send(newfd,buf,sizeof(buf),0);if(res2<0){ERR_MSG("send");return -1;}printf("发送成功\n");}//关闭文件描述符if(close(sfd)<0){ERR_MSG("close");return -1;}return 0;
}
TCP客服端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>#define PORT 8888
#define IP "192.168.124.38"#define ERR_MSG(msg) do{\fprintf(stderr,"line:%d\n",__LINE__);\fprintf(stderr,"func:%s\n",__func__);\perror(msg);\}while(0)int main(int argc, const char *argv[])
{//创建流式套节奏int cfd=socket(AF_INET,SOCK_STREAM,0);if(cfd<0){ERR_MSG("socket");return -1;}printf("流式套节奏创建完毕 cfd=%d\n",cfd);//填充服务器地址信息结构体给bind函数使用//真实的地址信息结构体根据地址族制度,AF_INET-->>man 7 ip/*struct sockaddr_in sin;sin.sin_family=AF_INET;//必须填AF_INETsin.sin_port=htons(PORT);//端口号的网络字节序 1024~49151sin.sin_addr.s_addr=inet_addr(IP);//本机IP,ifconfig,必须是桥接模式*/struct sockaddr_in sin2;sin2.sin_family=AF_INET;//必须填AF_INETsin2.sin_port=htons(PORT);//端口号的网络字节序 1024~49151sin2.sin_addr.s_addr=inet_addr(IP);//本机IP,ifconfig,必须是桥接模式//绑定客户端地址信息,不是必须绑定//未绑定由于电脑自动分配一个端口号/*if(bind(cfd,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("bing");return -1;}*///链接指定的服务器if(connect(cfd,(struct sockaddr*)&sin2,sizeof(sin2))<0){ERR_MSG("connect");return -1;}printf("连接服务器[%s:%d]成功\n",IP,PORT);char buf[128]="hello";ssize_t res;char buf2[128]="";ssize_t res2;while(1){bzero(buf,sizeof(buf2)); //发送printf("请输入:");fgets(buf2,sizeof(buf2),stdin);res2=send(cfd,buf2,sizeof(buf2),0);if(res2<0){ERR_MSG("send");return -1;}printf("发送成功\n");//接收bzero(buf,sizeof(buf));res=recv(cfd,buf,sizeof(buf),0);if(res<0){ERR_MSG("recv");return -1;}else if(res==0){printf("[%s:%d]cfd=%d服务器下线\n",IP,PORT,cfd);break;}printf("[%s:%d]newfd=%d : %s\n",IP,PORT,cfd,buf);}//关闭文件描述符if(close(cfd)<0){ERR_MSG("close");return -1;}return 0;
}
UDP服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 6666
#define IP "192.168.124.39"#define ERR_MSG(msg) do{\fprintf(stderr,"line:%d",__LINE__);\fprintf(stderr,"func:%s",__func__);\perror(msg);\
}while(0)int main(int argc, const char *argv[])
{//创建报式套节字int sfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd<0){ERR_MSG("sfd");return -1;}printf("报式套接字创建成功,sfd=%d\n",sfd);//允许端口快速被复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速重用\n");//填充服务器的地址信息结构体给bind函数使用//真实的地址信息结构体根据地址族制度,AF_INET-->>man 7 ipstruct sockaddr_in sin;sin.sin_family=AF_INET;//必须填AF_INETsin.sin_port=htons(PORT);//端口号的网络字节序 1024~49151sin.sin_addr.s_addr=inet_addr(IP);//本机IP,ifconfig,必须是桥接模式//绑定服务器的地址信息,必须绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("bing");return -1;}printf("绑定成功\n");char buf[128]="";struct sockaddr_in cin;//存储接收到的对端的地址信息socklen_t addrlen=sizeof(cin);while(1){bzero(buf,sizeof(buf));//接收数据//if(recvfrom(sfd,buf,sizeof(buf),0,NULL,NULL)<0){if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen)<0){ERR_MSG("recvrom");return -1;}printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);//发送数据strcat(buf,"*-*");if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0){ERR_MSG("sendto");return -1;}printf("发送成功\n");}//关闭套接字文件描述符close(sfd);return 0;
}
UDP客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 6666
#define IP "192.168.124.39"#define ERR_MSG(msg) do{\fprintf(stderr,"line:%d",__LINE__);\fprintf(stderr,"func:%s",__func__);\perror(msg);\
}while(0)int main(int argc, const char *argv[])
{//创建报式套节字int sfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd<0){ERR_MSG("sfd");return -1;}printf("报式套接字创建成功,sfd=%d\n",sfd);//允许端口快速被复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速重用\n");//填充服务器的地址信息结构体给bind函数使用//真实的地址信息结构体根据地址族制度,AF_INET-->>man 7 ip//填充对端地址信息结构体,给sestruct sockaddr_in sin;sin.sin_family=AF_INET;//必须填AF_INETsin.sin_port=htons(PORT);//端口号的网络字节序 1024~49151sin.sin_addr.s_addr=inet_addr(IP);//本机IP,ifconfig,必须是桥接模式//绑定服务器的地址信息,必须绑定/*if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("bing");return -1;}printf("绑定成功\n");*/char buf[128]="";char buf2[128]="";struct sockaddr_in cin;//存储接收到的对端的地址信息socklen_t addrlen=sizeof(cin);while(1){bzero(buf2,sizeof(buf));//发送数据printf("请输入:");fgets(buf2,sizeof(buf2),stdin);buf[strlen(buf)-1]='\0';if(sendto(sfd,buf2,sizeof(buf2),0,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("sendto");return -1;}printf("发送成功\n");bzero(buf,sizeof(buf));//接收数据//if(recvfrom(sfd,buf,sizeof(buf),0,NULL,NULL)<0){if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen)<0){ERR_MSG("recvrom");return -1;}printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);}//关闭套接字文件描述符close(sfd);return 0;
}