linux c语言socket编程实例

1.服务器端代码:

#include 
#include 
#include 
#include 

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

#define SERVER_PORT 10004

using namespace std;

#define MAXLINE 1024

int main() {
	int listenfd, connfd;
	struct sockaddr_in sockaddr;
	struct sockaddr_in clientaddr;
	int addr_len = sizeof(clientaddr);
	char buff[MAXLINE];
	int n;
	memset(&sockaddr, 0, sizeof(sockaddr));
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	sockaddr.sin_port = htons(SERVER_PORT);

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	bind(listenfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
	listen(listenfd, 10);
//	printf("Wait for the client information\n");
//	printf("监听端口:%d\n", SERVER_PORT);

	while(1){
		printf("监听端口:%d\n", SERVER_PORT);
		// clientAddr是一个传出参数,accept返回时,传出客户端的地址和端口号
		// addr_len是一个传入-传出参数,传入的是调用者提供的缓冲区的clientAddr的长度,以免缓冲区溢出
		// 传出的是一个客户端地址结构体的实际长度
		if((connfd = accept(listenfd, (struct sockaddr*)&clientaddr, (socklen_t*)&addr_len)) == -1){
			printf("accpect socket error: %s errno :%d \n", strerror(errno), errno);
		}
		printf("等待消息...\n");
		printf("IP is %s\n", inet_ntoa(clientaddr.sin_addr));
		printf("Port is %d\n", htons(clientaddr.sin_port));

		while(1){
			printf("读取消息:");
			n = recv(connfd, buff, MAXLINE, 0);
			buff[n] = '\0';
			printf("recv msg from client:  %s\n", buff);
			if(strcmp(buff, "quit") == 0){
				break;
			}
			printf("发送消息:\n");
			char resp[MAXLINE] = "return from server";
			if((send(connfd, resp, strlen(resp), 0)) < 0){
				printf("send mes error: %s errno : %d",strerror(errno), errno);
				exit(0);
			}
			if(strcmp(resp, "quit") == 0){
				break;
			}
		}
	}
	close(listenfd);
	return 0;
}

 

2.客户端:

#include 

#include 
#include 

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

using namespace std;

#define MAXLINE 1024
#define SERVER_PORT 10004

int main() {
	//socket client init
	char addr[] = "127.0.0.1";
	char* servInetAddr = addr;
//	char* servInetAddr = "127.0.0.1";
	int socketfd;
	struct sockaddr_in sockaddr;
	char recvline[MAXLINE], sendline[MAXLINE];

	//原型 int socket(int domain, int type, int protocol)
	//domain表示要使用的协议簇,AF_UNIX(本机通信)AF_INET(TCP/IP - IPv4) AF_INET(IPv6)
	//type参数指的是套接字类型 :SOCK_STREAM(TCP流) SOCK_DGRAM(UDP数据报) SOCK_RAW(原始套接字)
	//如果前两个参数确定了那么protocol参数设置为0
	//如果创建成功则返回套接字,失败则返回-1,并且错误代码写入errno
	socketfd = socket(AF_INET, SOCK_STREAM, 0);

	memset(&sockaddr, 0, sizeof(sockaddr));    //memset from string.h
	sockaddr.sin_family = AF_INET;    //sin_family from netinet/in.h
	sockaddr.sin_port = htons(10004);
	inet_pton(AF_INET, servInetAddr, &sockaddr.sin_addr);  // inet_pton from arpa/inet.h
	if((connect(socketfd, (struct sockaddr*) &sockaddr, sizeof(sockaddr)))<0){   //连接到服务器
		printf("connect error %s errno: %d\n", strerror(errno),errno);
		exit(0);
	}
	printf("连接到服务器...\n");
	//end socket client init

	while(1){
		printf("发送消息...\n");
		scanf("%s", sendline);
//		strcpy(sendline, "hello");
		if((send(socketfd, sendline, strlen(sendline), 0))<0){
			printf("send message error: %s errno : %d", strerror(errno), errno);
			exit(0);
		}else{
			printf("send message to server successful\n");
		}
		if(strcmp(sendline, "quit")==0){
			break;
		}
		int bytesReceived = 0;
		bytesReceived = recv(socketfd, recvline, MAXLINE, 0);
		string RespStr = recvline;
		RespStr = RespStr.substr(0,bytesReceived);
		cout<<"recv msg from server:"+RespStr<

参考教程:https://blog.csdn.net/jinmie0193/article/details/78951055

你可能感兴趣的:(c,c++)