Linux C语言TCP客户端程序

流程图如下

Linux C语言TCP客户端程序_第1张图片

 

实例代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 


int main(int argc, char* argv[])
{
	if(argc<3)
	{
		printf("usage : %s ip port\n", argv[0]);
		exit(1);
	}

	// create socket
	// tcp
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd<0)
	{
		perror("socket error");
		exit(1);
	}

	struct sockaddr_in serveraddr;
	memset(&serveraddr,0, sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	inet_pton(AF_INET, argv[1],
			&serveraddr.sin_addr.s_addr);
	if(connect(sockfd, (struct sockaddr*)&serveraddr,
				sizeof(serveraddr))<0)
	{
		perror("connect error");
		exit(1);
	}

	char buffer[1024];
	memset(buffer, 0, sizeof(buffer));
	size_t size;
	if((size = read(sockfd, buffer, sizeof(buffer)))<0)
	{
		perror("read error");
	}

	if(write(STDOUT_FILENO, buffer,size)!=size)
	{
		perror("write error");
	}

	close(sockfd);

	return 0;
}

 

你可能感兴趣的:(算法)