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 ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror("msg");\
}while(0);
#define IP "192.168.124.62"
#define PORT 8888
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);
//填充服务器的地址信息结构体
//真实的地址信息结构体根据地址族来制定
struct sockaddr_in sin;
//写一个包含ip端口的构造体
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("[%s:%d]服务器连接成功\n",IP,PORT);
char buf[128];
ssize_t res;
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 = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(res == 0)
{
printf("[%s:%d]服务器下线\n",IP,PORT);
break;
}
printf("[%s:%d] newfd=%d : %s\n",IP,PORT,cfd,buf);
}
close (cfd);
return 0;
}
服务器端
#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 //1014~49151
#define IP "192.168.124.62"
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
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_LNET-->>man 7 ipp
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器的地址信息,必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功!\n");
//将套接字设置尾监听状态
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("accept");
return -1;
}
printf("[%s:%d] newfd=%d 客户端连接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//接收
char buf[128];
ssize_t res;
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);
//发送
printf("请输入>>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
//关闭
close (newfd);
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
UDP
服务器端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 6666
#define IP "192.168.124.62"
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int fd = socket(AF_INET,SOCK_DGRAM,0);
if(fd<0)
{
ERR_MSG("scoket");
return -1;
}
printf("创建流式套接字成功\n");
//允许端口被重复使用
int reuse = 1;
if(setsockopt(fd,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);
if(bind(fd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
//接收数据
char arr[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1)
{
bzero(arr,sizeof(arr));//清空arr数据
if(recvfrom(fd,arr,sizeof(arr),0,(struct sockaddr*)&cin,&addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] arr=%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),arr);
//发送数据
strcat(arr,"hello word");
if(sendto(fd,arr,sizeof(arr),0,\
(struct sockaddr*)&cin,sizeof(cin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
}
close(fd);
return 0;
}
客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 6666
#define IP "192.168.124.62"
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int fd = socket(AF_INET,SOCK_DGRAM,0);
if(fd<0)
{
ERR_MSG("scoket");
return -1;
}
printf("创建流式套接字成功cd=%d\n",fd);
/* //允许端口被重复使用
int reuse = 1;
if(setsockopt(fd,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);
/* //绑定IP及端口号
if(bind(fd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
*/
char arr[128];
struct sockaddr_in cin;
socklen_t addrlen = sizeof(arr);
while(1)
{
bzero(arr,sizeof(arr));//清空arr数据
//发送数据
printf("请输入数据>>>>>");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1] = '\0';
if(sendto(fd,arr,sizeof(arr),0,\
(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
bzero(arr,sizeof(arr));
//接收数据
if(recvfrom(fd,arr,sizeof(arr),0,\
(struct sockaddr*)&cin,&addrlen)<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] arr=%s\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),arr);
}
close(fd);
return 0;
}