socket编程实例TCP

时间:2022-03-10 10:24:52

Socket编程简单实例,TCP链接,服务器返回客户端发送内容!


客户端代码:tcpclient.cpp

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define trace printf
#define MAX_BUFFER_SIZE 128

int main(int argc, char* argv[])
{
	int port = 0;
	
	if(argc != 4)
	{
		trace("params error !\n");
		trace("example:10.12.2.250 8000 hello\n");
		return 0;
	}

	for(int ii = 0; ii < argc; ii++)
	{
		trace("***param %d: %s\n", ii + 1, argv[ii]);
	}

	port = atoi(argv[2]);
	
	int sock = -1, ret = -1, recv_len = 0;
	struct sockaddr_in server;

	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = inet_addr(argv[1]);

	if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		trace("create socket error !\n");
		return 0;
	}
	
	if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
	{
		trace("connect server error !\n");
		close(sock);
		return 0;
	}
	trace("connect server success !\n");

	char buffer[MAX_BUFFER_SIZE] = {0};
	strncpy(buffer, argv[3], sizeof(buffer) - 1);

	if((send(sock, buffer, strlen(buffer) + 1, 0)) == -1)
	{
		trace("send buffer error !\n");
		close(sock);
		return 0;
	}
	trace("send buffer success !\n");

	memset(buffer, 0, sizeof(buffer));
	if((recv_len = recv(sock, buffer, sizeof(buffer), 0)) != -1)
	{
		trace("recv buffer success len = %d!\n", recv_len);
		
		trace("server respond:%s\n", buffer);
	}
	else
	{
		trace("recv buffer error !\n");
	}

	trace("close sock success !\n");
	close(sock);
	return 0;
}

服务器代码:tcpserver.cpp

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define trace printf
#define MAX_BUFFER_SIZE 128

int main(int argc, char* argv[])
{
	int port = 0;
	
	if(argc != 2)
	{
		trace("params error , need assign port!\n");
		return 0;
	}
	
	port = atoi(argv[1]);

	int sock = -1, sock_connect = -1;
	struct sockaddr_in server;
	
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = htonl(INADDR_ANY);

	if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		trace("create socket error !\n");
		return 0;
	}
	
	if(bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
	{
		trace("socket bind error !\n");
		close(sock);
		return -1;
	}
	
	if(listen(sock, 10) != 0)
	{
		trace("socket listen error !\n");
		close(sock);
		return -1;
	}
	trace("socket wait connect......\n");

	int recv_len = 0;
	char buffer[MAX_BUFFER_SIZE] = {0};

	while((sock_connect = accept(sock, NULL, NULL)) != -1)
	{
		if((recv_len = recv(sock_connect, buffer, sizeof(buffer), 0)) != -1)
		{
			trace("recv buffer success len = %d!\n", recv_len);
			trace("recv contents:%s\n", buffer);

			if((send(sock_connect, buffer, recv_len, 0)) == -1)
			{
				trace("send buffer error !\n");
			}
		}
		
		close(sock_connect);
		sock_connect = -1;
		trace("\nsocket wait connect......\n");
	}

	return 0;
}

运行结果:

server:./tcpserver 8000

client:./tcpclient 10.12.2.250 8000 hello

***param 1: ./tcpclient
***param 2: 10.12.2.250
***param 3: 9898
***param 4: hello
connect server success !
send buffer success !
recv buffer success len = 6!
server respond:hello
close sock success !

server:

socket wait connect......
recv buffer success len = 6!
recv contents:hello