您的位置:首页 > 理论基础 > 计算机网络

Linux下C语言实现TCP通信

2016-04-28 10:22 489 查看
TCP编程:

写一个tcp的服务端,一个tcp客户端。

客户端连接到服务端,然后每隔2s向服务端发送消息:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define SERVER_IP "127.10.10.11"
#define PORT 12345
#define MAXDATASIZE 512

int main()
{

int fd;
int num;

char recebuf[MAXDATASIZE];
char sendbuff[200];
int number = 1;

struct hostent *he;   //the struct that will get information about remote host
struct sockaddr_in  server;

if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create socket error!\n");
exit(1);
}

bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
//server.sin_addr = *((struct in_addr *)he->h_addr);
server.sin_addr.s_addr = inet_addr(SERVER_IP);

//start to conncet
if(connect(fd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("connect error\n");
exit(1);
}

while(1)
{

//start to send message
snprintf(sendbuff, sizeof(sendbuff), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
<request id=\"%d\" type=\"connect\">\n
<say>I am client!</say>\n
</request>"
,number);
//printf("%s\n", sendbuff);
num = send(fd, sendbuff, sizeof(sendbuff), 0);

if(num == -1)
{
printf("send message error\n");
}

//start to receive message
num = recv(fd, recebuf, MAXDATASIZE, 0);
if(num == -1)
{
printf("receive message error\n");
exit(-1);
}
recebuf[num] = '\0';
printf("\n\n接收到的回复是:\n");
printf("%s\n\n", recebuf);

sleep(2);
number++;

}
close(fd);

return 0;
}


下面是服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define PORT 12345
#define MAXDATASIZE 512

#define BACKLOG 1

void setresponse(char rece[], char send[])
{
char str1[] = "request";
char str2[] == "client";

char dest1[] = "response";
char dest2[] = "server";

int i1 = 0, i2 = 0;
int j1 = 0, j2 = 0;

int flag_send = 0;
int flag_dest1 = 0, flag_dest2 = 0;

//match the str request
while(rece[i1] != '\0')
{
if(rece[i1] == str1[j1])
{
i1++;
j1++;
if(str1[j1] == '\0')
{
break;
}
}
else
{
i1++;
j1 = 0;
}
}

//match the str client
while(rece[i2] != '\0')
{
if(rece[i2] == str2[j1])
{
i2++;
j2++;
if(str2[j2] == '\0')
{
break;
}
}
else
{
i2++;
j2 = 0;
}
}

//start to conncet the str before response
while(flag_send < (i1-j1))
{
send[flag_send] = rece[flag_send];
flag_send++;
}

// start to connect the str response
while(dest1[flag_dest1] != '\0')
{
send[flag_send] = dest1[flag_dest1];
flag_dest1++;
flag_send++;
}

//start to connect the str between response and server
while(i1 < (i2-j2))
{
send[flag_send] = rece[i1+1];
i1++;
flag_send++;
}

// start to connect the str server
while(dest2[flag_dest2] != '\0')
{
send[flag_send] = dest2[flag_dest2];
flag_dest2++;
flag_send++;
}

//start to connect the str after server
while(rece[i2] != '\0')
{
send[flag_send] = rece[i2];
flag_send++;
i2++;
}
send[flag_send] = '\0';
}

int main()
{
char recebuf[MAXDATASIZE];
char sendbuff[200];
int listenfd, connectfd;

struct sockaddr_in server; //server's address information
struct sockaddr_in client;  // client's address information

socklen_t addrlen;

/*create tcp socket*/
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create socket failed\n");
exit(1);
}

//set socket options
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);

//bind socket
if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("bind error\n");
exit(1);
}

//listen scoket
if(listen(listenfd, BACKLOG) == -1)
{
printf("listen error\n");
exit(1);
}

addrlen = sizeof(client);

while(1)
{
//conncet the socket
if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen)) == -1)
{
printf("accept error\n");
exit(1);
}

struct timeval tv;
gettimeofday(&tv, NULL);
printf("you got a connnetion from client's ip %s, port %d at time %ld.%ld\n", inet_ntoa(client.sin_addr), htons(client.sin_port),
tv.tv_sec, tv.tv_usec);

int iret = -1;
while(1)
{
iret = recv(connectfd, recebuf, MAXDATASIZE, 0);
if(iret > 0)
{
printf("%s\n", recebuf);
}
else
{
close(connectfd);
break;
}
setresponse(recebuf, sendbuff);
send(connectfd, recebuf, iret, 0);
}
}

close(listenfd);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: