tcp 服务器
#include<myhead.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d: %s %s",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0)
#define IP "192.168.2.174"
#define PORT 7777
int main(int argc, const char *argv[])
{
//创建流试套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{ ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体,给bind函数绑定
//真实的地址信息结构体根据地址族制定: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
//绑定服务器的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("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//存储客户端的地址信息结构体
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
//获取连接成功后新的套接字文件描述符
int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("客户端连接成功 newfd=%d __%d__\n",newfd,__LINE__);
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("客户端下线 newfd=%d __%d__\n",newfd,__LINE__);
break;
}
printf("newfd=%d: %s\n",newfd,buf);
//发送
strcat(buf,"");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
close(newfd);
close(sfd);
return 0;
}
tcp 客户端
#include<myhead.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d: %s %s",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0)
#define IP "192.168.2.174"
#define PORT 7777
int main(int argc, const char *argv[])
{
//创建流试套接字
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{ ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体,给connect函数绑定
//客户端想连接那个服务器,就填哪个服务器绑定的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(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;
while(1)
{
bzero(buf,sizeof(buf));
//发送
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 __%d__\n",__LINE__);
bzero(buf,sizeof(buf));
//接收
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("服务器下线 cfd=%d __%d__\n",cfd,__LINE__);
break;
}
printf("%s __%d__\n",buf,__LINE__);
}
close(cfd);
return 0;
}
udp 服务器
#include<myhead.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d: %s %s",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0)
#define IP "192.168.2.174"
#define PORT 7777
int main(int argc, const char *argv[])
{
//创建报试套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{ ERR_MSG("socket");
return -1;
}
printf("create sock success sfd=%d __%d__\n",sfd,__LINE__);
//填充服务器的地址信息结构体,给bind函数绑定
//真实的地址信息结构体根据地址族制定: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
//绑定服务器的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);
//发送数据,谁发给我,我发给谁
strcat(buf,"");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
//关闭套接字
close(sfd);
return 0;
}
udp 客户端
#include<myhead.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d: %s %s",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0)
#define IP "192.168.2.174"
#define PORT 7777
int main(int argc, const char *argv[])
{
//创建报试套接字
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{ ERR_MSG("socket");
return -1;
}
printf("create sock success cfd=%d __%d__\n",cfd,__LINE__);
//填充服务器的地址信息结构体,给bind函数绑定
//真实的地址信息结构体根据地址族制定: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]="";
struct sockaddr_in cin;//存储数据包从哪里来
socklen_t addrlen=sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
//发送数据,谁发给我,我发给谁
// strcat(buf,"");
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("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
//接收数据
if(recvfrom(cfd,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(cfd);
return 0;
}