资源简介

本文介绍了在Windows 操作系统下基于TCP/IP 协议Socket 套接口的通信机制以及多线程编程知识与技巧,并给出多线程方式实现多用户与服务端(C/S)并发通信模型的详细算法,最后展现了用C++编写的多用户与服务器通信的应用实例并附有程序。 关键词:Windows;套接字;多线程;并发服务器; Socket 是建立在传输层协议(主要是TCP 和UDP)上的一种套接字规范,最初由美国加州Berkley 大学提出,为UNIX 系统开发的网络通信接口,它定义了两台计算机之间通信的规范,socket 屏蔽了底层通信软件和具体操作系统的差异,使得任何两台安装了TCP 协议软件和实现了Socket 规范的计算机之间的通信成为可能,Socket 接口是TCP/IP 网络最为通用的应用接口,也是在Internet 上进行网络程序应用开发最通用的API[1],本文介绍了Socket通信的基本机制以及采用多线程技术实现并发通信的基本原理,并给出实例。

资源截图

代码片段和文件信息

#include “stdafx.h“
#include “Socket.h“

#pragma comment(lib“WS2_32.LIB“)


//********************************************************************************************************/
//** 函数名 ** SocketInit()
//**  输入  ** 无
//**  输出  ** 无
//**函数描述** 加载套接字
//********************************************************************************************************/
int SocketInit()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested=MAKEWORD(22);
err = WSAStartup(wVersionRequested&wsaData); //返回0,成功,否则就是错误码

if (err!=0)
{
printf(“WinSock DLL版本不足要求n“);
return 0;
}

if (LOBYTE(wsaData.wVersion)!=2||
HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
return 0;
}

return 1;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      32256  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\Debug\SocketClient.exe

     文件     365276  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\Debug\SocketClient.ilk

     文件     568320  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\Debug\SocketClient.pdb

     文件       8616  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\BuildLog.htm

     文件         65  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\mt.dep

     文件      25268  2015-04-06 16:21  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\Socket.obj

     文件        663  2015-04-06 16:21  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\SocketClient.exe.embed.manifest

     文件        728  2015-04-06 16:21  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\SocketClient.exe.embed.manifest.res

     文件        621  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\SocketClient.exe.intermediate.manifest

     文件      30491  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\SocketClient.obj

     文件    3211264  2015-04-06 16:21  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\SocketClient.pch

     文件      12821  2015-04-06 16:21  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\stdafx.obj

     文件     486400  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\vc90.idb

     文件     176128  2015-04-07 14:47  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Debug\vc90.pdb

     文件       1213  2015-03-30 16:15  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\ReadMe.txt

     文件        786  2015-04-06 16:01  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Socket.cpp

     文件        988  2015-04-06 16:01  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\Socket.h

     文件       2979  2015-04-07 14:58  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\SocketClient.cpp

     文件       4635  2015-04-06 16:01  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\SocketClient.vcproj

     文件       1427  2015-04-07 14:58  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\SocketClient.vcproj.USER-20140331IT.Administrator.user

     文件       1427  2015-03-30 22:00  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\SocketClient.vcproj.USER-20141117OX.Administrator.user

     文件       2029  2015-04-06 15:56  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\SocketSever.cpp

     文件        217  2015-03-30 16:15  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\stdafx.cpp

     文件        233  2015-03-30 16:15  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\stdafx.h

     文件        498  2015-03-30 16:15  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient\targetver.h

     文件   11324416  2015-03-30 22:12  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient.ncb

    ..A..H.     10752  2015-03-30 22:12  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClient.suo

     文件   11348992  2015-04-07 14:58  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClientThread.ncb

     文件        902  2015-03-30 16:15  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClientThread.sln

    ..A..H.     10240  2015-04-07 14:58  Socket(TCP)\SocketClientThread\SocketClientThread\SocketClientThread.suo

............此处省略43个文件信息

评论

共有 条评论