网络编程__第一个服务端和客户端

最近开始学习自习网络编程了,按照的是课本<<TCP/IP网络编程>>,这本书 很适合学完c++之后学生,真是一本很好的书

看完前几章后我写完了第一个服务端和客户端

但是传输的数据结果不对,看了好久也没找出来


//server.cpp

#include"iostream"

//1.连接头文件winsock2.h和动态链接库ws2_32.lib
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
//end_1


using namespace std;

void ErrorHanding(char* message);

int main(int argc,char* argv[]){
	WSADATA wsa_data;
	SOCKET server_socket,client_socket;
	SOCKADDR_IN server_addr,client_addr;
	int size_client_addr;
	char message[]="hello world";
	
	if(argc!=2){
		cout<<"Usage: "<<argv[0]<<" <port>"<<endl;
		exit(1);
	}
	
	//2对winsocket的初始化,int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData);
	//wVersionRequested是版本信息,lpWSAData是一个指向WSAData型的指针,WSAData作用不清楚
	if(WSAStartup(MAKEWORD(2,2),&wsa_data)!=0){//WSAStartup调用成功返回0
		ErrorHanding("WSAStartup() error!");
		exit(1);
	}
	//end_2

	//3创建套接字
	server_socket=socket(PF_INET,SOCK_STREAM,0);
	if(server_socket==INVALID_SOCKET){
		ErrorHanding("socket() error!");
		exit(1);
	}
	//end_3
	
	//4服务端地址的初始化
	memset(&server_addr,0,sizeof(server_addr));//server_addr赋值为0
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=htonl(INADDR_ANY);//本机地址,是一个10进制
	server_addr.sin_port=htons(atoi(argv[1]));//atoi将字符串转化成整形,转化成10进制
	//end_4
	
	//5bind()
	if(bind(server_socket,(SOCKADDR*) &server_addr,sizeof(server_addr))==SOCKET_ERROR){//(SOCKADDR*)不止对应是ip4,也可以对应ip6
																					   //所以SOCKADDR_IN(ip4)和SOCKADDR对应内存模型一致
		ErrorHanding("bind() error!");
		exit(1);
	}
	//end_5
	
	//6listen()
	if(listen(server_socket,5)==SOCKET_ERROR){
		ErrorHanding("listen() error!");
		exit(1);
	}
	//end_6
	
	//7accept()拿到client信息
	size_client_addr=sizeof(client_addr);
	cout<<"begin:"<<size_client_addr<<endl;
	client_socket=accept(server_socket,(SOCKADDR*) &client_socket,&size_client_addr);//???&size_client_addr为什么要的是这个地址
																		//accept调用后将client地址大小放在&size_client_addr里,accept对ip4和ip6都有效
	cout<<"begin:"<<size_client_addr<<endl;
	if(client_socket==INVALID_SOCKET){
		ErrorHanding("accept() error!");
		exit(1);
	}
	//end_7
	
	//8send()
	
	send(client_socket,message,sizeof(message),0);//int send( SOCKET s, const char FAR *buf, int len, int flags );  第4个参数一般是0
	//end_8
	closesocket(server_socket);
	closesocket(client_socket);
	WSACleanup();//对winsock的注销
	return 0;
}

void ErrorHanding(char* message){
	fputs(message,stderr);
	fputc('\n',stderr);
}

//client.cpp

#include"iostream"
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include "string"
using namespace std;

void ErrorHanding(char* message);

int main(int argc,char* argv[]){
	WSADATA wsa_data;
	SOCKET client_socket;
	SOCKADDR_IN server_addr;
	char message[30];
	
	
	if(argc!=3){
		cout<<"Usage: "<<argv[0]<<" <IP> <port>"<<endl;
		exit(1);
	}
	
	if(WSAStartup(MAKEWORD(2,2),&wsa_data)!=0){
		ErrorHanding("WSAStartup() error!");
		exit(1);
	}
	

	
	client_socket=socket(PF_INET,SOCK_STREAM,0);
	if(client_socket==INVALID_SOCKET){
		ErrorHanding("socket() error!");
		exit(1);
	}

	
	
	memset(&server_addr,0,sizeof(server_addr));
	server_addr.sin_family=AF_INET;
	//server_addr.sin_addr.s_addr=htonl(INADDR_ANY);//服务端
	server_addr.sin_addr.s_addr=inet_addr(argv[1]);//和server端相比,这里传递的是字符串,用inet_addr()转化
	server_addr.sin_port=htons(atoi(argv[2]));

	if(connect(client_socket,(SOCKADDR*) &server_addr,sizeof(server_addr))==SOCKET_ERROR){
		ErrorHanding("socket() error!");
		exit(1);
	}
	
	int strlen=recv(client_socket,message,sizeof(message)-1,0);
	if(strlen==-1){
		ErrorHanding("recv() error!");
		exit(1);
	}
	cout<<message<<endl;
	
	closesocket(client_socket);
	WSACleanup();
	return 0;
}

void ErrorHanding(char* message){
	fputs(message,stderr);
	fputc('\n',stderr);
}

//server.cpp

//client.cpp


传到client的数据不是hello world!

你可能感兴趣的:(网络编程__第一个服务端和客户端)