创建TCP服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.124.118"
#define PORT 5050
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
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);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
fprintf(stderr, "line:%d ", __LINE__);
perror("setsockopt");
return -1;
}
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
if(listen(sfd, 128) < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("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)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("accept");
return -1;
}
printf("accept success,newfd=%d __%d__\n",newfd,__LINE__);
char buf[128];
ssize_t res;
ssize_t rec;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("recv");
return -1;
}
else if(0 == res)
{
printf("客户端下线\n");
break;
}
printf("newfd=%d : %s __%d__\n",newfd,buf,__LINE__);
rec = send(newfd,buf,sizeof(buf),0);
if(rec < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("send");
return -1;
}
else if(rec > 0)
{
printf("send success\n");
}
printf("newfd=%d : %s __%d__\n",newfd,buf,__LINE__);
}
close(newfd);
close(sfd);
return 0;
}
客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define CL_IP "192.168.124.118"
#define CL_PORT 5050
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("创建流式套接字文件成功\n");
int reuse = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
fprintf(stderr, "line:%d ", __LINE__);
perror("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(CL_PORT);
sin.sin_addr.s_addr = inet_addr(CL_IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
fprintf(stderr, "line:%d ", __LINE__);
perror("connect");
return -1;
}
printf("connect success\n");
char buf[128]="";
ssize_t res;
while(1)
{
printf("请输入数据\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] ='\0';
if(send(cfd,buf,sizeof(buf),0) < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("send");
return -1;
}
printf("send success\n");
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线\n");
break;
}
printf("cfd=%d : %s __%d__\n",cfd,buf,__LINE__);
}
close(cfd);
return 0;
}