TCP服务端搭建
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
//=====服务端代码=======
#define ERR_MSG(msg) do{\
fprintf(stdout,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.31.232"
int main(int argc, const char *argv[])
{
//套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//端口快速被重用
int resue = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
//绑定
if(bind(cfd, (struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
//监听
if(listen(cfd,120) < 0)
{
ERR_MSG("listen");
return -1;
}
//接受
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd=-1;
newfd = accept(cfd ,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
char buf[128] = "";
ssize_t res = -1;
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)
{
fprintf(stderr,"[ %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);
//向客户端发送数据
strcat(buf,"*_*");
if(send(newfd, buf, sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
}
//关闭文件描述符
close(newfd);
close(cfd);
return 0;
}
TCP客户端搭建
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
//=====客户端代码=======
#define ERR_MSG(msg) do{\
fprintf(stdout,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.31.232"
int main(int argc, const char *argv[])
{
//套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//端口快速被重用
int resue = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
//绑定
if(connect(cfd, (struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
char buf[128] = "";
ssize_t res = -1;
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;
}
//服务端数据接收
bzero(buf,sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(0 == res)
{
printf("服务端关闭\n");
break;
}
printf(":%s\n",buf);
}
//关闭文件描述符
close(cfd);
return 0;
}