背景知识
关于项目转换:VC 6.0工程向VS 2010转换的问题
网络编程基础:WS网络开发——第1篇
TCP程序
实现:该例子由客户端和服务器组成。实现的功能是客户端向服务器发送“MyTCP"字符串,服务器接收并显示该字符串。客户端发送数据后退出,服务器显示数据后退出。
服务器和客户端调用Windows Sockets API的过程如下图:
工程截图:
两个工程中的stdafx.h与stdafx.cpp源码相同
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__E01B4F25_9E02_42E0_9E68_09D08D1C776F__INCLUDED_)
#define AFX_STDAFX_H__E01B4F25_9E02_42E0_9E68_09D08D1C776F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__E01B4F25_9E02_42E0_9E68_09D08D1C776F__INCLUDED_)
stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// Server.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
Server.cpp
// Server.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#define BUF_SZIE 64
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsd; //WSADATA变量
SOCKET sServer; //服务器套接字
SOCKET sClient; //客户端套接字
SOCKADDR_IN addrServ;; //服务器地址
char buf[BUF_SZIE]; //接收数据缓冲区
int retVal; //返回值
//初始化套结字动态库
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
//创建套接字
sServer = socket(AF_INET, SOCK_STREAM, 0);
if(INVALID_SOCKET == sServer)
{
printf("socket failed!\n");
WSACleanup();//释放套接字资源;
return -1;
}
//服务器套接字地址
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(4999);
addrServ.sin_addr.s_addr = INADDR_ANY;
//绑定套接字
retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
if(SOCKET_ERROR == retVal)
{
printf("bind failed!\n");
closesocket(sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return -1;
}
//开始监听
retVal = listen(sServer, 1);
if(SOCKET_ERROR == retVal)
{
printf("listen failed!\n");
closesocket(sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return -1;
}
//接受客户端请求
sockaddr_in addrClient;
int addrClientlen = sizeof(addrClient);
sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen);
if(INVALID_SOCKET == sClient)
{
printf("accept failed!\n");
closesocket(sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return -1;
}
//接收客户端数据
ZeroMemory(buf, BUF_SZIE);
retVal = recv(sClient, buf, BUF_SZIE, 0);
if (SOCKET_ERROR == retVal)
{
printf("recv failed!\n");
closesocket(sServer); //关闭套接字
closesocket(sClient); //关闭套接字
WSACleanup(); //释放套接字资源;
return -1;
}
printf("%s\n", buf); //输出"MyTCP"
//退出
closesocket(sServer); //关闭套接字
closesocket(sClient); //关闭套接字
WSACleanup(); //释放套接字资源;
return 0;
}
Client.cpp
// Client.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#define BUF_SZIE 64
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsd; //WSADATA变量
SOCKET sHost; //服务器套接字
SOCKADDR_IN servAddr; //服务器地址
char buf[BUF_SZIE]; //接收数据缓冲区
int retVal; //返回值
//初始化套结字动态库
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
return -1;
}
//创建套接字
sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == sHost)
{
printf("socket failed!\n");
WSACleanup();//释放套接字资源
return -1;
}
//设置服务器地址
servAddr.sin_family =AF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servAddr.sin_port = htons((short)4999);
int nServAddlen = sizeof(servAddr);
//连接服务器
retVal=connect(sHost,(LPSOCKADDR)&servAddr, sizeof(servAddr));
if(SOCKET_ERROR == retVal)
{
printf("connect failed!\n");
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return -1;
}
//向服务器发送数据
ZeroMemory(buf, BUF_SZIE);
strcpy(buf, "MyTCP");
retVal = send(sHost, buf, strlen(buf), 0);
if (SOCKET_ERROR == retVal)
{
printf("send failed!\n");
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return -1;
}
//退出
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return 0;
}
执行结果:
源文件下载地址:http://download.csdn.net/detail/fengdongjingquan/6460763