TCP服务器和客户端
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.12"
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket create success 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");
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定服务器的地址信息-->必须绑定 bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态 listen
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//获取一个客户端信息,生产一个新的文件描述符 accept
int newfd=-1;
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
ERR_MSG("newfd");
return -1;
}
printf("[%s:%d]客户端连接成功 newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
char buf[128]="";
ssize_t res=0;
while(1){
//清空字符串
bzero(buf,sizeof(buf));
//接收
res=recv(newfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
return -1;
}else if(0==res){
printf("[%s:%d]客户端下线 newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d]客户端连接成功 newfd=%d : %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
//发送
strcat(buf,">_<");
if(send(newfd,buf,sizeof(buf),0)<0){
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//关闭文件名描述符
close(newfd);
if(close(sfd)<0){
ERR_MSG("close");
return -1;
}
return 0;
}
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.12"
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
//绑定客户端地址信息---->非必须绑定
//填充服务器的地址信息结构体给connect函数连接
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//连接指定服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128]="";
ssize_t res=0;
while(1){
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
//发送
if(send(cfd,buf,sizeof(buf),0)<0){
ERR_MSG("send");
return -1;
}
printf("send success\n");
bzero(buf,sizeof(buf));
//接收
if((res=recv(cfd,buf,sizeof(buf),0))<0){
ERR_MSG("recv");
return -1;
}else if(res==0){
printf("服务器下线 cfd=%d\n",cfd);
break;
}
printf("cfd=%d : %s\n",cfd,buf);
}
//关闭文件名描述符
if(close(cfd)<0){
ERR_MSG("close");
return -1;
}
return 0;
}
UDP服务器和客户端
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.12"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d\n",sfd);
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//绑定地址信息结构体 bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success\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,(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);
//发送数据
strcat(buf,">_<");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
//关闭套接字
close(sfd);
return 0;
}
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.12"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//绑定地址信息结构体 bind 非必须绑定
char buf[128]="";
ssize_t res=0;
struct sockaddr_in rcv;
socklen_t addrlen=sizeof(rcv);
while(1){
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
//发送数据
if((sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)))<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
bzero(buf,sizeof(buf));
//接收数据
if((res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv,&addrlen))<0){
ERR_MSG("recvfrom");
return -1;
}else if(res==0){
printf("服务器下线 cfd=%d\n",cfd);
break;
}
printf("cfd=%d : %s\n",cfd,buf);
}
//关闭套接字
close(cfd);
return 0;
}