TCP服务端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#define ERR_MSG(msg) do{\
printf("LINE:__%d__\n",__LINE__);\
printf("FUNC:__%s__\n",__func__);\
perror(msg);\
}while(0)
#define PORT 6666 //端口号 1024~49151
#define IP "192.168.124.14"
int main(int argc, const char *argv[])
{
//chuangjian流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功\n");
//允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //将端口(2个字节用htons)号转换成网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //将IP号转换成网络字节序
//绑定服务器IP和端口
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
printf("%d\n",errno);
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] = "";
int res = 0;
printf("服务器开始接收\n");
while(1)
{
bzero(buf, sizeof(buf));
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("revc");
return -1;
}
printf("[%s:%d] buf = %s\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
if(0 == res)
{
printf("[%s:%d] newfd = %d 客户端退出链接\n",\
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
return -1;
}
if(strcmp(buf, "quit") == 0)
{
printf("服务器接受端退出\n");
break;
}
}
//发送
int set = 0;
while(1)
{
bzero(buf, sizeof(buf));
printf("buf>>>");
scanf("%s",buf);
set = send(newfd, buf, sizeof(buf), 0);
if(set < 0)
{
ERR_MSG("send");
return -1;
}
else if(strcmp(buf, "quit") == 0)
{
printf("服务器写段退出\n");
break;
}
}
return 0;
}
TCP客服端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#define ERR_MSG(msg) do{\
printf("LINE:__%d__\n",__LINE__);\
printf("FUNC:__%s__\n",__func__);\
perror(msg);\
}while(0)
#define PORT 6666 //端口号 1024~49151
#define IP "192.168.124.14"
int main(int argc, const char *argv[])
{
//chuangjian流式套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //将端口(2个字节用htons)号转换成网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //将IP号转换成网络字节序
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("[%s;%d] cfd = %d 服务器连接成功\n",IP, PORT, cfd);
//接收
char buf[128] = "";
int res = 0;
int set = 0;
printf("********************\n");
printf("客服端发送端接入\n");
while(1)
{
bzero(buf, sizeof(buf));
printf("buf>>>");
scanf("%s",buf);
set = send(cfd, buf, sizeof(buf), 0);
if(set < 0)
{
ERR_MSG("send");
return -1;
}
if(strcmp(buf, "quit") == 0)
{
printf("客服端发送端退出\n");
break;
}
}
printf("********************\n");
printf("客服端接收端接入\n");
while(1)
{
bzero(buf, sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("revc");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] 服务器退出连接\n",IP, PORT);
return 0;
}
if(strcmp(buf, "quit") == 0)
{
printf("接受端退出\n");
break;
}
printf("buf = %s\n",buf);
}
return 0;
}
UDP服务端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#define ERR_MSG(msg) {\
printf("LINE:__%d__\n",__LINE__);\
printf("FUNC:__%s__\n",__func__);\
perror(msg);\
}
#define IP "192.168.124.14"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建套接字
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功\n");
int reuse = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
if(errno != 98)
{
ERR_MSG("bind");
return -1;
}
}
printf("bin is success\n");
struct sockaddr_in cin;
socklen_t addlen = sizeof(cin);
int res = 0;
char buf[128] = "";
printf("***\n ");
while(1)
{
bzero(buf, sizeof(buf));
res = recvfrom(fd, buf, sizeof(buf),0, (struct sockaddr*)&cin, &addlen);
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, "*+*");
int len = sendto(fd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, addlen);
if(len < 0)
{
ERR_MSG("sendto");
return -1;
}
}
return 0;
}
UDP客户端
#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_MDG(msg) {\
printf("LINE:__%d__\n",__LINE__);\
perror(msg);\
}
#define IP "192.168.124.14"
#define PORT 8888
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(cfd < 0)
{
ERR_MDG("socket");
return -1;
}
printf("socket is success\n");
char buf[128] = "";
int res = 0;
int len = 0;
//目标端的信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_addr.s_addr = inet_addr(IP);
cin.sin_port = htons(PORT);
while(1)
{
//发送信息 w
//
bzero(buf, sizeof(buf));
printf("buf>>>");
scanf("%s",buf);
getchar();
if(sendto(cfd, buf, sizeof(buf), 0, (struct sockadrr*)&cin, sizeof(cin)) < 0)
{
ERR_MDG("sendto");
return -1;
}
printf("法送成功 %s\n",buf);
//收信息
bzero(buf, sizeof(buf));
len = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL );
if(len < 0)
{
ERR_MDG("recvform");
return -1;
}
printf("buf = %s\n",buf);
}
return 0;
}