WinSock网络编程学习(一)Echo客户/服务器程序

Echo Protocol,应答协议。该协议主要用于调试和检测中。可以通过

   Echo是互联网上的一个标准协议,它是一个非常有用的调试和测量工具,Echo服务器简单的把收到的任何消息发给客户端。它可以基于TCP协议,服务器就在TCP端口7(知名端口号)检测有无消息,如果使用UDP,过程也一样。   发送echo数据包知道当前的连接点有哪些路径,也可以获得路径长度。

 客户端程序为EchoClnt.c.

#include<stdio.h>
#include<winsock2.h>	//contains window.h

#pragma comment(lib,"ws2_32")	//winsock's lib,it's a Precompiled command.


#define ECHO_DEF_PORT	7	//Default port
#define ECHO_BUF_SIZE	256 // the size of buff

/*
argv[ ]: 字符串数组,用来存放指向你的字符串参数的指针数组,每一个元素指向一个参数
argv[0] 指向程序运行的全路径名
argv[1] 指向在DOS命令行中执行程序名后的第一个字符串
argv[2] 指向执行程序名后的第二个字符串
*/
int main(int argc,char **argv)	
{
	WSADATA wsa_data;
	SOCKET echo_soc = 0;			//socket's handle
	struct sockaddr_in serv_addr;	//server's addr
	unsigned short port = ECHO_DEF_PORT;
	int result = 0, send_len = 0;
	char *test_data = "Hello world !",recv_buf[ECHO_BUF_SIZE];
	if(argc < 2)					// command line's input's number .warn:add the way(E:\test\Echo.exe 127.0.0.1)
	{
		printf("input %s server_address [port]\n%d",argv[0],argc);
		return -1;
	}
	if(argc >= 3)
		port = atoi(argv[2]);		

	WSAStartup(MAKEWORD(2,0),&wsa_data);	//initial winsock 
	send_len = strlen(test_data);
	//server's address
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(port);
	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
	if(serv_addr.sin_addr.s_addr == INADDR_NONE)
	{
		printf("[ECHO] invaild address\n");
		return -1;
	}
	echo_soc = socket(AF_INET,SOCK_STREAM,0);// Creat a socket
	result = connect(echo_soc,(struct sockaddr*)&serv_addr,sizeof(serv_addr));	//connect to server
	if(result == 0)
	{
		result = send(echo_soc,test_data,send_len,0);	//send message to serv.
		result = recv(echo_soc,recv_buf,ECHO_BUF_SIZE,0);	//receive message
	}
	if(result > 0)
	{
		recv_buf[result] = 0;
		printf("[Echo Client] error : %d.\r\n",WSAGetLastError());
	}
	closesocket(echo_soc);	//realse source
	WSACleanup();
	return 0;
} 
服务器程序:

#include<stdio.h>
#include<winsock2.h>	//contains window.h

//server.c
#pragma comment(lib,"ws2_32")	//winsock's lib,it's a Precompiled command.


#define ECHO_DEF_PORT	7	//Default port
#define ECHO_BUF_SIZE	256 // the size of buff

int main(int argc,char **argv)	
{
	WSADATA wsa_data;
	SOCKET echo_soc = 0,	//listening socket		 
		   acpt_soc = 0;
	struct sockaddr_in serv_addr,	/*server's addr*/
					clnt_addr;
	unsigned short port =ECHO_DEF_PORT;
	int result = 0;
	int addr_len = sizeof(struct sockaddr_in);
	char recv_buf[ECHO_BUF_SIZE]; 
	if(argc == 2)
	{
		 port = atoi(argv[1]);
		 printf("sadw");
	}

	WSAStartup(MAKEWORD(2,0),&wsa_data);	//initial winsock

	echo_soc = socket(AF_INET,SOCK_STREAM,0);

	 
	//server's local address
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(port);
	serv_addr.sin_addr.s_addr = INADDR_ANY;

	result = bind(echo_soc,(struct sockaddr*)&serv_addr,sizeof(serv_addr));

	// it's the constant  several behavior.
	if(result == SOCKET_ERROR)
	{
		printf("[ECHO SERVER] bind error :%d\n",WSAGetLastError());
		closesocket(echo_soc);
		return -1;
	}

	listen(echo_soc,SOMAXCONN);


	printf("[ECHO ERVER]is running....\n");
	while(1)
	{
		acpt_soc = accept(echo_soc,(struct sockaddr*)&clnt_addr,&addr_len);
		if(acpt_soc == INVALID_SOCKET)
		{
			printf("[ECHO SERVER] accept error:%d\n",WSAGetLastError());
			break;
		}
		result = recv(acpt_soc,recv_buf,ECHO_BUF_SIZE,0);
		if(result > 0)
		{
			recv_buf[result] = 0;
			printf("[ECHO SERVER]receives: \"%s\",from %s\r\n",recv_buf,inet_ntoa(clnt_addr.sin_addr));
			result = send(acpt_soc,recv_buf,result,0);
		}
			closesocket(acpt_soc);
	}
	closesocket(echo_soc); 
	WSACleanup();
	return 0;
} 
测试程序要先启动服务器程序,然后在命令行输入:

>(客户端程序路径)EchoClnt.exe 127.0.0.1

 (注:代码和部分文字解析来自--《winsock网络编程经络》,这里我加上了一些个人理解,对书中程序和知识进行小小的总结。网友们笑看便是。)

你可能感兴趣的:(互联网,服务器)