tcp--服务器
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.5.53" //本机ip
#define PORT 6667
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd =socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("scoket");
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");
//填充地址信息结构体
//真实的地址信息结构体根据地址族指定 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); //本机ip ifconfig
//将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("[%s:%d] newfd=%d 连接成功__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),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("[%s:%d] newfd=%d 客户端下线__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
break;
}
printf("[%s:%d] newfd=%d :%s__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
//关闭
close(newfd);
close(sfd);
return 0;
}
tcp--客户端
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.5.53" //本机ip
#define PORT 6667
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd =socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("scoket");
return -1;
}
printf("cfd=%d\n",cfd);
//允许端口快速重用
int reuse =1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功\n");
//填充地址信息结构体
//真实的地址信息结构体根据地址族指定 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); //本机ip ifconfig
//非必须绑定
//connect 连接服务器
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));
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("发送成功\n");
bzero(buf,sizeof(buf));
//接收
res= read(cfd,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("cfd=%d :%s__%d__\n",cfd,buf,__LINE__);
}
//关闭
close(cfd);
return 0;
}
udp----服务器
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.5.53" //本机ip
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd =socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("scoket");
return -1;
}
printf("sfd=%d\n",sfd);
//真实的地址信息结构体根据地址族指定 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); //本机ip ifconfig
//将IP和端口绑定到套字节上,必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//存储数据包是从谁哪里来的
struct sockaddr_in cin;
socklen_t addrlen =sizeof(cin);
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收
res= recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<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("发送成功\n");
}
//关闭
close(sfd);
return 0;
}
udp---客户端
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.5.53" //本机ip
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd =socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("scoket");
return -1;
}
printf("sfd=%d\n",cfd);
//真实的地址信息结构体根据地址族指定 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); //本机ip ifconfig
#if 0
//将IP和端口绑定到套字节上,必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
#endif
struct sockaddr_in rcvaddr;
socklen_t addrlen =sizeof(rcvaddr);
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(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
bzero(buf,sizeof(buf));
//接收
res= recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
}
//关闭
close(cfd);
return 0;
}