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

📄 client.cpp

📁 java socket server,对初学者很好的参考意义
💻 CPP
字号:
/*    a C++ socket client class   Keith Vertanen 11/98   */#define BACKLOG 10      // how many pending connections queue will hold #define VERBOSE 1      // turn on or off debugging output#include "client.h"Client::Client(int p, int datap, char *myhost, int rev){	port = p;	dataport = datap;		        if (VERBOSE) printf("Server: opening socket to %s on port = %d, datagrams on port = %d\n",				myhost, port, dataport);	if ((he=gethostbyname(myhost))==NULL) 		perror("gethostbyname");        if ((new_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)             perror("socket");	if ((datasockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {            perror("datagram socket");            exit(1);        }        if ((senddatasockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {            perror("datagram send socket");            exit(1);        }	        my_addr.sin_family = AF_INET;                my_addr.sin_port = htons(dataport);         my_addr.sin_addr.s_addr = INADDR_ANY;         bzero(&(my_addr.sin_zero), 8);                their_addr.sin_family = AF_INET;                their_addr.sin_port = htons(port);              their_addr.sin_addr = *((struct in_addr *) he->h_addr);         bzero(&(their_addr.sin_zero), 8);                if (connect(new_fd, (struct sockaddr *)&their_addr, sizeof(struct		sockaddr)) == -1) {            perror("connect new_fd");            exit(1);        }        if (bind(datasockfd, (struct sockaddr *)&my_addr, sizeof(struct                sockaddr)) == -1) {            perror("bind datagram socket");	    exit(1);        }	// send out request for reversed bits or not	char temp[1];	if (rev)	{		temp[0] = 1;		send(new_fd, temp, 1, 0);	}	else	{		temp[0] = 0;		send(new_fd, temp, 1, 0);	}};// send a string to the socketvoid Client::send_string(char *str){        if (send(new_fd, (char *) str, strlen(str), 0) == -1)            perror("send");              	if (VERBOSE) printf("Server: sending string '%s'\n", str);                       	recv_ack();	send_ack();};// send some integers over the wirevoid Client::send_ints(int *val, int len){	int *buff;	char *ptr, *valptr;	int i,j;	// we may need to reverse the byte order, oh joy	if (REVERSE)	{		// set the buff pointer to our previously allocated spot		buff = (int *) buffer;		ptr = (char *) buff;		valptr = (char *) val;		// we need to reverse the order of each set of bytes		for (i = 0; i < len; i++)		{			for (j=0; j<sizeof(int); j++)				ptr[i*sizeof(int)+j] = (char)					valptr[(i+1)*sizeof(int)-j-1];		}        	if (send(new_fd, (char *) buff, sizeof(int)*len, 0) == -1)            		perror("send ints");              			}	else        	if (send(new_fd, (char *) val, sizeof(int)*len, 0) == -1)            		perror("send ints");              	if (VERBOSE)	{ 		printf("Server: sending %d ints - ", len);                       		for (i=0; i<len; i++)			printf("%d ", val[i]);		printf("\n");	}	// don't continue until we receive an ack from client	recv_ack();	send_ack();};// send some floats over the wirevoid Client::send_floats(float *val, int len){	float *buff;	char *ptr, *valptr;	int i,j;	// we may need to reverse the byte order, oh joy	if (REVERSE)	{		buff = (float *) buffer;		ptr = (char *) buff;		valptr = (char *) val;		// we need to reverse the order of each set of bytes		for (i = 0; i < len; i++)		{			for (j=0; j<sizeof(float); j++)				ptr[i*sizeof(float)+j] = (char)					valptr[(i+1)*sizeof(float)-j-1];		}        	if (send(new_fd, (char *) buff, sizeof(float)*len, 0) == -1)            		perror("send floats");              			}	else        	if (send(new_fd, (char *) val, sizeof(float)*len, 0) == -1)            		perror("send floats");              	if (VERBOSE)	{ 		printf("Server: sending %d floats - ", len);                       		for (i=0; i<len; i++)			printf("%0.3f ", val[i]);		printf("\n");	}	recv_ack();	send_ack();};// send some doubles over the wirevoid Client::send_doubles(double *val, int len){	double *buff;	char *ptr, *valptr;	int i,j;	// we may need to reverse the byte order, oh joy	if (REVERSE)	{		// allocate a temporary array to hold the reversed bytes		buff = (double *) buffer;		ptr = (char *) buff;		valptr = (char *) val;		// we need to reverse the order of each set of bytes		for (i = 0; i < len; i++)		{			for (j=0; j<sizeof(double); j++)				ptr[i*sizeof(double)+j] = (char)					valptr[(i+1)*sizeof(double)-j-1];		}        	if (send(new_fd, (char *) buff, sizeof(double)*len, 0) == -1)            		perror("send doubles");              			}	else        	if (send(new_fd, (char *) val, sizeof(double)*len, 0) == -1)            		perror("send doubles");              	if (VERBOSE)	{ 		printf("Server: sending %d doubles - ", len);                       		for (i=0; i<len; i++)			printf("%0.3f ", val[i]);		printf("\n");	}	recv_ack();	send_ack();};// send some bytes over the wirevoid Client::send_bytes(char *val, int len){        int i;                if (send(new_fd, (char *) val, len, 0) == -1)                        perror("send bytes");                if (VERBOSE)        {                printf("Server: sending %d bytes - ", len);                for (i=0; i<len; i++)                        printf("%d ", val[i]);                printf("\n");        }                // don't continue until we receive an ack from client                recv_ack();         send_ack();};// send a packet of bytes using a datagramvoid Client::send_datagram(char *val, int len){        int i, numbytes;        if ((numbytes=sendto(senddatasockfd, val, len, 0,                (struct sockaddr *) &their_addr, sizeof(struct sockaddr)))==-1)                        perror("send datagram bytes");        if (VERBOSE)        {                printf("Server: sending datagram of %d bytes - ", len);                for (i=0; i<len; i++)                        printf("%d ", val[i]);                printf("\n");        }};// receive a string, returns num of bytes receivedint Client::recv_string(char *str, int maxlen, char terminal){	int numbytes = 0;	int end = 0;	int i, j;	char *temp;	// set the temp buffer to our already allocated spot	temp = (char *) buffer;	j = 0;	// this is annoying, but the java end is sending a char	// at a time, so we recv some chars (probably 1), append	// it to our str string, then carry on until we see	// the terminal character	while (!end)	{		if ((numbytes=recv(new_fd, temp, BUFFSIZE, 0))==-1)			perror("recv");		for (i=0; i<numbytes; i++)		{			str[j] = temp[i];				j++;		}		if ((temp[i-1]==terminal) || (j==maxlen-1))			end = 1;	}	str[j] = '\0';	if (VERBOSE) printf("Server: received '%s'\n", str);                       		send_ack();	recv_ack();		return numbytes;};// receive some ints, returns num of ints receivedint Client::recv_ints(int *val, int maxlen){	int numbytes = 0;	int i, j;	char *temp;        char *result;	int end = 0;	int total_bytes = 0;	temp = (char *) buffer;	result = (char *) buffer2;	j = 0;	// we receiving the incoming ints one byte at a time	// oh cross language sockets are so much fun...	while (!end)	{		if ((numbytes=recv(new_fd, temp, BUFFSIZE, 0))==-1)			perror("recv");		for (i=0; i<numbytes; i++)		{			result[j] = temp[i];				j++;		}		total_bytes = total_bytes + numbytes;		if (total_bytes==maxlen*sizeof(int))			end = 1;	}	// now we need to put the array of bytes into the array of ints	char *ptr;	int num = j/sizeof(int);	ptr = (char *) val;	// the significance order depends on the platform	if (REVERSE)	{		// we need to reverse the order of each set of bytes		for (i = 0; i < num; i++)		{			for (j=0; j<sizeof(int); j++)				ptr[i*sizeof(int)+j] = (char)					result[(i+1)*sizeof(int)-j-1];		}	}	else	{		// leave the byte order as is		for (i = 0; i < j; i++)		{			ptr[i] = result[i];		}	}	if (VERBOSE) 	{		printf("Server: received %d ints - ", num);             			for (i=0; i<num; i++)			printf("%d ", val[i]);		printf("\n");	}	send_ack();	recv_ack();	return num;};// receive some floats, returns num of floats receivedint Client::recv_floats(float *val, int maxlen){	int numbytes = 0;	int i, j;	char *temp;        char *result;	int end = 0;	int total_bytes = 0;	temp = (char *) buffer;	result = (char *) buffer2;	j = 0;	// we receiving the incoming ints one byte at a time	// oh cross language sockets are so much fun...	while (!end)	{		if ((numbytes=recv(new_fd, temp, BUFFSIZE, 0))==-1)			perror("recv");		for (i=0; i<numbytes; i++)		{			result[j] = temp[i];				j++;		}		total_bytes = total_bytes + numbytes;		if (total_bytes==maxlen*sizeof(float))			end = 1;	}	// now we need to put the array of bytes into the array of floats	char *ptr;	int num = j/sizeof(float);	ptr = (char *) val;	// the significance order depends on the platform	if (REVERSE)	{		// we need to reverse the order of each set of bytes		for (i = 0; i < num; i++)		{			for (j=0; j<sizeof(float); j++)				ptr[i*sizeof(float)+j] = (char)					result[(i+1)*sizeof(float)-j-1];		}	}	else	{		// leave the byte order as is		for (i = 0; i < j; i++)		{			ptr[i] = result[i];		}	}	if (VERBOSE) 	{		printf("Server: received %d floats - ", num);             			for (i=0; i<num; i++)			printf("%f ", val[i]);		printf("\n");	}	send_ack();	recv_ack();	return num;};// receive some doubles, returns num of doubles receivedint Client::recv_doubles(double *val, int maxlen){	int numbytes = 0;	int i, j;	char *temp;        char *result;	int end = 0;	int total_bytes = 0;	temp = (char *) buffer;	result = (char *) buffer2;	j = 0;	// we receiving the incoming ints one byte at a time	// oh cross language sockets are so much fun...	while (!end)	{		if ((numbytes=recv(new_fd, temp, BUFFSIZE, 0))==-1)			perror("recv");		for (i=0; i<numbytes; i++)		{			result[j] = temp[i];				j++;		}		total_bytes = total_bytes + numbytes;		if (total_bytes==maxlen*sizeof(double))			end = 1;	}	// now we need to put the array of bytes into the array of floats	char *ptr;	int num = j/sizeof(double);	ptr = (char *) val;	// the significance order depends on the platform	if (REVERSE)	{		// we need to reverse the order of each set of bytes		for (i = 0; i < num; i++)		{			for (j=0; j<sizeof(double); j++)				ptr[i*sizeof(double)+j] = (char)					result[(i+1)*sizeof(double)-j-1];		}	}	else	{		// leave the byte order as is		for (i = 0; i < j; i++)		{			ptr[i] = result[i];		}	}	if (VERBOSE) 	{		printf("Server: received %d doubles - ", num);             			for (i=0; i<num; i++)			printf("%e ", val[i]);		printf("\n");	}	send_ack();	recv_ack();	return num;};// receive some ints, returns num of ints receivedint Client::recv_bytes(char *val, int maxlen){        int numbytes = 0;        int i, j;        char *temp;         int end = 0;        int total_bytes = 0;                                temp = (char *) buffer;        j = 0;         // we receiving the incoming ints one byte at a time        // oh cross language sockets are so much fun...                while (!end)        {                if ((numbytes=recv(new_fd, temp, BUFFSIZE, 0))==-1)                        perror("recv");                for (i=0; i<numbytes; i++)                {                        val[j] = temp[i];                        j++;                }                                total_bytes = total_bytes + numbytes;                if (total_bytes==maxlen)                        end = 1;                if (total_bytes==maxlen)                        end = 1;                }                if (VERBOSE)        {                printf("Server: received %d bytes - ", maxlen);                for (i=0; i<maxlen; i++)                        printf("%d ", val[i]);                printf("\n");        }                         send_ack();        recv_ack();                         return maxlen;};// receive a datagramint Client::recv_datagram(char *val, int maxlen){        int numbytes = 0;        int addr_len, i;	struct sockaddr_in from_addr;        addr_len = sizeof(struct sockaddr);        if ((numbytes=recvfrom(datasockfd, val, maxlen, 0,                (struct sockaddr *) &from_addr, &addr_len)) == -1)        {                perror("recvfrom datagram");        }        if (VERBOSE)        {                printf("Server: received datagram of %d bytes - ", numbytes);                 for (i=0; i<numbytes; i++)                        printf("%d ", val[i]);                printf("\n");        }        return numbytes;};                // shut down the socketvoid Client::closesocket(){	close(new_fd);};// recv a short ack from the client void Client::recv_ack(){	char temp[1];	int total = 0;	if (VERBOSE)		printf("Waiting for ack...\n");	while (total<1)	   total += recv(new_fd, temp, 1, 0);		if (VERBOSE)		printf("Ack recieved.\n");};// send a short ack to the client void Client::send_ack(){	char temp[1];	temp[0] = 42;	if (VERBOSE)		printf("Sending ack...\n");      	send(new_fd, temp, 1, 0);};

⌨️ 快捷键说明

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