TCP Server&Client socket实现(Ubuntu16.04)

本文介绍在Ubuntu 16.04系统中使用Socket进行TCP服务器和客户端编程的方法。通过具体代码示例,详细讲解了创建Socket、设置Socket选项、绑定地址、监听连接、接受客户端请求、发送与接收数据的过程。
摘要由CSDN通过智能技术生成

TCP Server&Client socket实现(Ubuntu16.04)

1 测试:

在这里插入图片描述

2 TCP Server

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>//struct
#include <unistd.h>//close head
#include <string.h>//bzero
#define PORT 8111
#define MESSAGE_SIZE 1024
int main(int argc, char* argv[])
{
	int socket_fd;
	int accept_fd;
	int backlog = 10;
	int ret = -1;
	int flag = 1;
	struct sockaddr_in local_addr, remote_addr;
	char in_buf[MESSAGE_SIZE] = {0,};
	
	//create socket
	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (socket_fd == -1)
	{
		std::cout << "file to create socket!" << std::endl;
		exit(-1);
	}
	//set socket options
	ret = setsockopt(socket_fd, 
					SOL_SOCKET, 
					SO_REUSEADDR, 
					&flag, 
					sizeof(flag));
	if (ret == -1)
	{
		std::cout << "failed to set socket options!" << std::endl;
	}
	//set localaddr
	local_addr.sin_family = AF_INET;
	local_addr.sin_port = PORT;
	local_addr.sin_addr.s_addr = INADDR_ANY;//0 RENHE IP DOU LISTEN
	bzero(&(local_addr.sin_zero),8);
	//bind socket
	ret = bind(socket_fd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr));
	if (ret == -1)
	{
		std::cout << "failed to bind addr!" << std::endl;
		exit(-1);
	}
	//listen
	ret = listen(socket_fd, backlog);
	if (ret == -1)
	{
		std::cout << "failed to listen socket!" << std::endl;
		exit(-1);
	}
	for(;;)
	{
		socklen_t addr_len = sizeof(struct sockaddr);
		accept_fd  = accept(socket_fd, 
							(struct sockaddr *) &remote_addr, 
							&addr_len);
		for (;;)
		{
			ret = recv(accept_fd, (void *)in_buf, MESSAGE_SIZE, 0);
			if (ret == 0)
				break;
			std::cout << "receive: " << in_buf << std::endl;
		
			send(accept_fd, (void*)in_buf, MESSAGE_SIZE,0);
		}
		close(accept_fd);
	}
	close(socket_fd);
	return 0;
}

3 TCP Client

#include <iostream>
#include <sys/socket.h>//1connect
#include <sys/types.h>//2connect
#include <netinet/in.h> //struct
#include <string.h>//memset
#include <stdio.h>//gets
#include <unistd.h>//close
#include <arpa/inet.h>//inet
#define PORT 8111
#define MESSAGE_LEN 1024
using namespace std;
int main(int argc, char* argv[])
{
	int socket_fd;
	int ret = -1;
	char sendbuf[MESSAGE_LEN] = {0,};
	char recvbuf[MESSAGE_LEN] = {0,};
	struct sockaddr_in serverAddr;
	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (socket_fd < 0)
	{
		cout << "failed to create socket" << endl;
		exit(-1);
	}
	
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = PORT;
	serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	ret = connect(socket_fd, 
				(struct sockaddr *)&serverAddr, 
				sizeof(struct sockaddr));
	if (ret < 0)
	{
		cout << "failed to connect!" << endl;
		exit(-1);
	}
	while(1)
	{
		memset(sendbuf, 0, MESSAGE_LEN);
		gets(sendbuf);
		ret = send(socket_fd, sendbuf, strlen(sendbuf), 0);
		if (ret <= 0)
		{
			cout << "failed to send data!" << endl;
			break;		
		}
		//guanbi 1 kill -9 1111      2 duibi
		if (strcmp(sendbuf, "quit") == 0)
		{
			break;
		}
		ret = recv(socket_fd, recvbuf, MESSAGE_LEN, 0);
		recvbuf[ret] = '\0';
		cout << "recv:" << recvbuf << endl;
	}
	close(socket_fd);
	
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值