码迷,mamicode.com
首页 > 其他好文 > 详细

基于socket的简单网络程序设计

时间:2014-05-21 15:58:01      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:windows   mfc   socket   

基于socket的简单网络程序设计

1、实验目的与要求:

1)初步掌握TCPUDP方式的网络编程模式。

2)能运用Winsock提供的API函数接口进行网络程序的编写。

2、实验准备和说明:

1)要求掌握流式和数据报socket的编程模式和实现。

2)创建本次实验工作文件夹“…\实验\实验4”

3、实验内容与步骤:

      1)工程的创建

VC6里边新建一个工程,选择:Win32 Console Application,输入工程名。具体方法:启动VC6.0,进入【文件】->【新建】,然后在【新建工程】的对话框中选择:Win32 Console Application,输入一个工程名ntTcpClient及保存路径“…\实验\实验4”,点击【确定】

2Winsock的初始化

Windows环境下进行网络程序设计时,所有的Winsock函数都是从ws2_32.dll导出的,我们可以通过#pragma comment(lib, "ws2_32.lib")语句链接到该库文件。但在使用Winsock函数之前还必须调用WSAStartup函数对库资源进行初始化工作,使用完毕后,在退出程序之前,我们还必须调用WSACleanup函数来释放库资源。

3)新建文件ntTcpClient.cpp

<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>
#include "winsock2.h"

//winsock 2.2 library
#pragma comment(lib,"ws2_32.lib")

#define PORT  8888
#define ADDR  "127.0.0.1"

int main(int argc, char* argv[])
{
	WSADATA	wsock;
	SOCKET sconnection;
	SOCKADDR_IN          serAddr;
	int                  nRet=0;
	char                 buf[100];
	
	//初始化Winsock 2.2
	
	printf("\nInitialising Winsock...\n");
	if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
	{
		fprintf(stderr,"WSAStartup() failed %d\n, WSAGetLastError()");
        exit(0); 
	} 
	printf("Initialised successfully.\n");
	
	//创建监听Socket
	printf("\nCreating TCP Socket...\n");
	if ((sconnection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) 
		== INVALID_SOCKET)
	{
		printf("Creation of socket failed %d\n", WSAGetLastError());
		WSACleanup();
		return 0;
	}
	printf("TCP Socket Created successfully.\n");
	//设置OCKADDR_IN地址结构
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(PORT);    
	//serAddr.sin_addr.s_addr = INADDR_ANY;
	serAddr.sin_addr.s_addr = inet_addr(ADDR);
	//连接服务端
	printf("Connecting to %s:%d...\n",
		inet_ntoa(serAddr.sin_addr), htons(serAddr.sin_port));
	if (connect(sconnection, (SOCKADDR *) &serAddr, sizeof(serAddr)) 
		== SOCKET_ERROR)
	{
		printf("connection failed with error %d\n", WSAGetLastError());
		closesocket(sconnection);
		WSACleanup();
		return 0;
	} 
	printf("connection successfully.\n");
	printf("Trying to send  messages.\n");
	//收发处理
	for(int i=0;;i++)
	{
		memset(buf,0,sizeof(buf));
		printf("\nMessage to be sent:");
		gets(buf);
		if ((nRet = send(sconnection, buf, strlen(buf), 0)) == SOCKET_ERROR)
		{
			printf("send failed with error %d\n", WSAGetLastError());
			closesocket(sconnection);
			WSACleanup();
			return 0;
		}

		printf("The  data  was sent to %s successfully.\n",inet_ntoa(serAddr.sin_addr));
		if(strncmp(buf,"exit",sizeof("exit"))==0)
			break;
		memset(buf,0,sizeof(buf));

		if ((nRet = recv(sconnection, buf, sizeof(buf), 0)) 
			== SOCKET_ERROR)
		{
			printf("recv failed with error %d\n", WSAGetLastError());
		}
		printf("The following data  was received from %s successfully.\n",
			inet_ntoa(serAddr.sin_addr));
		printf(buf);
	}
	printf("Closing the connection.\n");

	closesocket(sconnection);
	WSACleanup();

	return 0;
}
</span>

4)编译运行

5)重建工程ntTcpServer

#include "stdafx.h"
#include <stdlib.h>
#include "winsock2.h"

//winsock 2.2 library
#pragma comment(lib,"ws2_32.lib")
 
#define PORT  8888
#define ADDR  "127.0.0.1"


int main(int argc, char* argv[])
{
	WSADATA	wsock;
	SOCKET listensocket,newconnection;
	SOCKADDR_IN          serAddr,cliAddr;
	int                  cliAddrLen=sizeof(cliAddr);
	int                  nRet=0;
	char                 buf[100];

	//初始化Winsock 2.2
	printf("\nInitialising Winsock...\n");
	if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
	{
		fprintf(stderr,"WSAStartup() failed %d\n, WSAGetLastError()");
        exit(0); 
	} 
	printf("Initialised successfully.\n");
	
	//创建监听Socket
	printf("\nCreating TCP Socket...\n");
	if ((listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) 
		== INVALID_SOCKET)
	{
		printf("Creation of socket failed %d\n", WSAGetLastError());
		WSACleanup();
		return 0;
	}
	printf("TCP Socket Created successfully.\n");
	//设置SOCKADDR_IN地址结构
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(PORT);    
	//   serAddr.sin_addr.s_addr = INADDR_ANY;
	serAddr.sin_addr.s_addr = inet_addr(ADDR);
	//绑定套接字
	if (bind(listensocket, (SOCKADDR *)&serAddr,
	   sizeof(serAddr))== SOCKET_ERROR)
	{
		printf("bind failed with error %d\n", WSAGetLastError());
		closesocket(listensocket);
		WSACleanup();
		return 0;
	}
	
	//监听连接
	if (listen(listensocket, 5) == SOCKET_ERROR)
	{
		printf("listen failed with error %d\n", WSAGetLastError());
		closesocket(listensocket);
		WSACleanup();
		return 0;
	} 
	
	printf("Waiting for a connection on port %d.\n", PORT);

	//接受连接
	if ((newconnection = accept(listensocket, (SOCKADDR *) &cliAddr,
		&cliAddrLen)) == INVALID_SOCKET)
	{
		printf("accept failed with error %d\n", WSAGetLastError());
		closesocket(listensocket);
		WSACleanup();
		return 0;
	}
	
	printf("successfully got a connection from %s:%d.\n",
		inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));

	//此时可以继续监听新的连接,或者停止监听进行数据收发
	closesocket(listensocket);
	
	printf("Waiting to receive data...\n");
	
	memset(buf,0,sizeof(buf));
	for(int i=0;;i++)
	{
		if ((nRet = recv(newconnection, buf, sizeof(buf), 0)) 
			== SOCKET_ERROR)
		{
			printf("recv failed with error %d\n", WSAGetLastError());
			closesocket(newconnection);
			WSACleanup();
			return 0;
		}
		//显示接收到的数据
		printf(buf);
		printf("\n");
		//若发现exit,则退出处理循环
		if(strncmp(buf,"exit",sizeof("exit"))==0)
		{
			printf("exit the receiving loop\n");
			break;
		}
		if((nRet = send(newconnection,buf,strlen(buf),0))
			==SOCKET_ERROR)
		{
			printf("send failed with error %d\n", WSAGetLastError());
		}
	} 
	//关闭已连接套接字
	closesocket(newconnection);
	
	WSACleanup();
	return 0;
}

6)编译运行,并尝试nTcpServernTcpClient进行通信

7)创建工程nUdpClientnUdpServer

//nUdpServer代码
#include <stdio.h>

#include "winsock2.h"

//winsock 2.2 library
#pragma comment(lib,"ws2_32.lib")

#define PORT  8888
#define ADDR  "127.0.0.1"

int main(int argc, char* argv[])
{
	WSADATA	wsock;
	SOCKET sersocket;
	SOCKADDR_IN  serAddr;
	int   serAddrsize=sizeof(serAddr);
	int   nRet=0;
	char  buf[1024];

	//初始化Winsock 2.2
	printf("\nInitialising Winsock...\n");
	if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
	{
		fprintf(stderr,"WSAStartup() failed %d\n, WSAGetLastError()");
        exit(0); 
	} 
	printf("Initialised successfully.\n");
	
	//创建socket
	printf("\nCreating UDP Socket...\n");
	if ((sersocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) 
		== INVALID_SOCKET)
	{
		printf("Creation of socket failed %d\n", WSAGetLastError());
		WSACleanup();
		return 0;
	}

	printf("UDP Socket Created successfully.\n");

	//设置SOCKADDR_IN地址结构
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(PORT);    
	//   serAddr.sin_addr.s_addr = INADDR_ANY;
	serAddr.sin_addr.s_addr = inet_addr(ADDR);
	//绑定套接字
	if (bind(sersocket, (SOCKADDR *)&serAddr,
		sizeof(serAddr))== SOCKET_ERROR)
	{
		printf("bind failed with error %d\n", WSAGetLastError());
		closesocket(sersocket);
		WSACleanup();
		return 0;
	}

	printf("Waiting to receive data...\n");

	for(int i=0;;i++)
	{
		if ((nRet = recvfrom(sersocket, buf, sizeof(buf), 0,
			(SOCKADDR *)&serAddr, &serAddrsize)) == SOCKET_ERROR)
		{
			printf("ERROR: recvfrom failed with error %d\n", WSAGetLastError());
			closesocket(sersocket);
			WSACleanup();
			return 0;
		}
		//显示接收到的数据
		printf(buf);
		printf("\n");
		if ((nRet = sendto(sersocket, buf, strlen(buf), 0, 
			(SOCKADDR *)&serAddr, sizeof(serAddr))) == SOCKET_ERROR)
		{
			printf("ERROR: sendto failed with error %d\n", WSAGetLastError());
			closesocket(sersocket);
			WSACleanup();
			return 0;
		}

		//若发现”exit”,则退出处理循环
		if(strncmp(buf,"exit",sizeof("exit"))==0)
		{
			printf("exit the receiving loop\n");
			break;
		}
	}

	//关闭套接字
	closesocket(sersocket);
	
	WSACleanup();
	return 0;
}

//nUdpClient代码
#include <stdio.h>
#include "winsock2.h"

//winsock 2.2 library
#pragma comment(lib,"ws2_32.lib")

#define PORT  8888
#define ADDR  "127.0.0.1"

int main(int argc, char* argv[])
{
	WSADATA	wsock;
	SOCKET clisocket;
	SOCKADDR_IN  serAddr;
	int   serAddrsize=sizeof(serAddr);
	int   nRet=0;
	char  buf[1024];

	//初始化Winsock 2.2
	printf("\nInitialising Winsock...\n");
	if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
	{
		fprintf(stderr,"WSAStartup() failed %d\n, WSAGetLastError()");
        exit(0); 
	} 
	printf("Initialised successfully.\n");
	
	//创建socket
	printf("\nCreating UDP Socket...\n");
	if ((clisocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) 
		== INVALID_SOCKET)
	{
		printf("Creation of socket failed %d\n", WSAGetLastError());
		WSACleanup();
		return 0;
	}
	
	//设置SOCKADDR_IN地址结构
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(PORT);    
	//   serAddr.sin_addr.s_addr = INADDR_ANY;
	serAddr.sin_addr.s_addr = inet_addr(ADDR);

	printf("UDP Socket Created successfully.\n");

	printf("Trying to send  messages.\n");
	for(int i=0;;i++)
	{
		memset(buf,0,sizeof(buf));
		printf("\nMessage to be sent:");
		gets(buf);
		if ((nRet = sendto(clisocket, buf,strlen(buf), 0, 
			(SOCKADDR *)&serAddr, sizeof(serAddr))) == SOCKET_ERROR)
		{
			printf("ERROR: sendto failed with error %d\n", WSAGetLastError());
			closesocket(clisocket);
			WSACleanup();
			return 0;
		}
		printf("The  data  was sent to %s successfully.\n",inet_ntoa(serAddr.sin_addr));
		if(strncmp(buf,"exit",sizeof("exit"))==0)
			break;
		memset(buf,0,sizeof(buf));

		if ((nRet = recvfrom(clisocket, buf, sizeof(buf), 0,
			(SOCKADDR *)&serAddr, &serAddrsize)) == SOCKET_ERROR)
		{
			printf("ERROR: recvfrom failed with error %d\n", WSAGetLastError());
			closesocket(clisocket);
			WSACleanup();
			return 0;
		}
		//显示接收到的数据
		printf("The following data  was received from %s successfully.\n",
			inet_ntoa(serAddr.sin_addr));
		printf(buf);
	}

	closesocket(clisocket);
	WSACleanup();
	return 0;
}

8)编译运行nUdpClientnUdpServer,并尝试在它们之间进行通信


基于socket的简单网络程序设计,布布扣,bubuko.com

基于socket的简单网络程序设计

标签:windows   mfc   socket   

原文地址:http://blog.csdn.net/cstopcoder/article/details/26400099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!