2.简单TPC客户端和服务器的搭建
客户端:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.155"//ifconfig出来的本机IP
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0){
ERR_MSG("socket");
return -1;
}
//绑定客户端自身的地址信息结构体---》非必须绑定
//填充服务器的地址信息结构体,给connect函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来的本机ip
//连接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("connect");
return -1;
}
printf("connect success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0,sen = 0;
while(1){
//发送
printf("客户端输入:");
scanf(" %s",buf);
sen = send(cfd,buf,strlen(buf),0);
if(sen==-1){
ERR_MSG("send");
return -1;
}
printf("send success__%d__",__LINE__);
bzero(buf,sizeof(buf));
//接收
res = recv(cfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
return -1;
}else if(res ==0){
printf("服务器下线__%d__\n",__LINE__);
break;
}
printf(":%s __%d__\n",buf,__LINE__);
}
//关闭套接文件描述符
close(cfd);
return 0;
}
服务器:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.155"//ifconfig出来的本机IP
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功 __%d__\n", __LINE__); struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来的本机ip
//绑定服务器的IP和端口----》必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success__%d__\n",__LINE__);
//将套接字设置为被动监听状态
if(listen(sfd,128)<0){
ERR_MSG("liseten");
return -1;
}
printf("listen success__%d__\n",__LINE__);
struct sockaddr_in cin;//存储客户端的地址信息结构体
socklen_t addrlen = sizeof(sin);
//获取连接成功的新套接字文件描述符
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]accept success__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),__LINE__);
char buf[128] = "";
ssize_t res = 0,sen = 0;
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]客户端下线 newfd=%d __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
break;
}
printf("[%s:%d]newfd=%d: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
printf("服务器输入:");
scanf(" %s",buf);
//发送
sen = send(newfd,buf,strlen(buf),0);
if(sen==-1){
ERR_MSG("send");
return -1;
}
printf("send success__%d__",__LINE__);
}
//关闭套接文件描述符
close(newfd);
close(sfd);
return 0;
}
3.简单UDP客户端和服务器的搭建
客户端:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.155"//ifconfig出来的本机IP
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
//绑定客户端自身的地址信息结构体----》非必须绑定
//填充服务器的地址信息结构体,给sendto函数使用
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来的本机ip
char buf[128] = "";
socklen_t addrlen = sizeof(sin);
while(1){
//发送数据
printf("客户端发出消息:");
scanf(" %s",buf);
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("sendto");
return -1;
}
printf("send success__%d__\n",__LINE__);
bzero(buf,sizeof(buf));
//接收
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen)<0){
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
}
//关闭套接字
close(sfd);
return 0;
}
服务器:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.155"//ifconfig出来的本机IP
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来的本机ip
//绑定地址信息结构体
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success__%d__\n",__LINE__);
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,(struct sockaddr*)&cin,&addrlen)<0){
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//发送数据
printf("服务器发出消息:");
scanf(" %s",buf);
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0){
ERR_MSG("sendto");
return -1;
}
printf("send success__%d__\n",__LINE__);
}
//关闭套接字
close(sfd);
return 0;
}