⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp~

📁 本代码是基于LINUX系统下的
💻 CPP~
字号:
#include <iostream>#include <stdio.h>#include <unistd.h>#include <errno.h>#include <sys/select.h>#include <string>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/socket.h>#include <sys/wait.h>#include <netdb.h>#define PORT 3490#define MAXDATASIZE 100using namespace std;int main(int argc, char *argv[]){	int sockfd;	int numbytes;	char buf[MAXDATASIZE] = "ni shi ge sha ";	fd_set readfd;	int len = 0;	int i = 0;	struct hostent *he;	struct sockaddr_in their_addr;	FD_ZERO(&readfd);	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)	{		perror("socket");		exit(1);	}	their_addr.sin_family = AF_INET;	their_addr.sin_port = htons(PORT);	their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");	bzero(&(their_addr.sin_zero), 8);	if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)	{		perror("connect");		exit(1);	}	send(sockfd, buf, 100, 0);	FD_ZERO(&readfd);//	FD_SET(sockfd, &readfd);//	select(2, &readfd, NULL, NULL, 0);	while(1)	{		cin.getline(buf, 100, '\n');			len = strlen(buf);		if((send(sockfd, buf, len, 0))== -1)		{			perror("send");			exit(1);		}		memset(buf, '\0', 100);		if(fork() == 0)		{			recv(sockfd, buf, 100, 0);			cout << "the message from sever:"<< buf<< endl;			exit(0);		}	//	FD_SET(sockfd, &readfd);	//	select(2, &readfd,NULL , NULL, 0);	/*	if(FD_ISSET(sockfd, &readfd))		{			if((recv(sockfd, buf, 100, 0)) == -1)			//perror("recv");			cout<< buf<< endl;			//FD_CLR(sockfd, &readfd);		}		FD_ZERO(&readfd);		//cin>> buf;			}*/	//buf[numbytes] = '\0';	//cout<< "received: "<< buf<< endl;//	close(sockfd);	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -