网络编程作业1

网络编程作业1_第1张图片

代码:

#include 

#define SERIP "192.168.125.135"
#define SERPOST 8888
#define CLIIP "192.168.125.155"
#define CLIPOST 8080

int main(int argc, const char *argv[])
{
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	if(cfd==-1){
		perror("socket error");
		return -1;
	}
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(CLIPOST);
	cin.sin_addr.s_addr=inet_addr(CLIIP);
	if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1){
		perror("bind error");
		return -1;
	}
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SERPOST);
	sin.sin_addr.s_addr=inet_addr(SERIP);
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1){
		perror("connect error");
		return -1;
	}
	char rbuf[]={0xff,0x02,0x00,0x00,0xff};
	unsigned char bbuf[]={0xff,0x02,0x01,0x00,0xff};
	char key;
	send(cfd,rbuf,sizeof(rbuf),0);
	send(cfd,bbuf,sizeof(bbuf),0);
	while(1){
		printf("please enter key:");
		scanf("%c",&key);
		getchar();
		switch(key){
		case 'w':
			rbuf[3]=rbuf[3]+0x10;
			send(cfd,rbuf,sizeof(rbuf),0);
			break;
		case 's':
			rbuf[3]=rbuf[3]-0x10;
			send(cfd,rbuf,sizeof(rbuf),0);
			break;
		case 'd':
			bbuf[3]=bbuf[3]+0x10;
			send(cfd,bbuf,sizeof(bbuf),0);
			break;
		case 'a':
			bbuf[3]=bbuf[3]-0x10;
			send(cfd,bbuf,sizeof(bbuf),0);
			break;
		case 'q':
		 	rbuf[3]=0x00;
			bbuf[3]=0x00;
			send(cfd,rbuf,sizeof(rbuf),0);
			send(cfd,bbuf,sizeof(bbuf),0);
			break;
		default :
			printf("enter error\n");
			break;
		}
	}
	close(cfd);

	return 0;
}

视频:

你可能感兴趣的:(网络)