TCP_SER
#include <myhead.h>
//定义处理错误的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d %s %s",__LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
//定义IP和端口号
#define IP "192.168.2.186"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1> 创建流式套接字socket
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//2> 绑定服务器ip和端口bind
//真实地址信息结构体
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;
}
//3> 设置为被动监听状态listen
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
//4> 获取连接成功的套接字accept
//获取接收的地址信息结构体
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);
while(1)
{
//5> 接收recv
char buf[128] = "";
//清
bzero(buf, sizeof(buf));
//收
ssize_t res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
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);
//6> 发送send
strcat(buf, "***");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
//7> 关闭
close(newfd);
close(sfd);
return 0;
}
TCP_CLI
#include <myhead.h>
//定义处理错误的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d %s %s",__LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
//定义IP和端口号
#define IP "192.168.2.186"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1> 创建流式套接字socket
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//真实地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//2> 连接服务器connect
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
while(1)
{
//3> 发送send
char buf[128] = "";
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) -1 ] = '\0';
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
//4> 接收recv
//清
bzero(buf, sizeof(buf));
//收
ssize_t res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
printf("服务器下线\n");
break;
}
printf("cfd = %d : %s\n", cfd, buf);
}
//5> 关闭
close(cfd);
return 0;
}
UDP_SER
#include <myhead.h>
//定义处理错误的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d %s %s",__LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
//定义IP和端口号
#define IP "192.168.2.186"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1> 创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//2> 绑定服务器
//真实地址信息结构体
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;
}
while(1)
{
char buf[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//3> 接收
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);
//4> 发送
strcat(buf, "***");
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, addrlen) < 0)
{
ERR_MSG("sendto");
return -1;
}
}
//5> 关闭
close(sfd);
return 0;
}
UDP_CLI
#include <myhead.h>
//定义处理错误的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d %s %s",__LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
//定义IP和端口号
#define IP "192.168.2.186"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1> 创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//2> 绑定服务器
//真实地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
while(1)
{
char buf[128] = "";
socklen_t addrlen = sizeof(sin);
//3> 发送
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, addrlen) < 0)
{
ERR_MSG("send");
return -1;
}
bzero(buf, sizeof(buf));
//4> 接收
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen) < 0)
{
ERR_MSG("recv");
return -1;
}
printf("[%s:%d] : %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
}
//5> 关闭
close(sfd);
return 0;
}