資源簡介
利用WinSock來實現雙機通信,理解TCP狀態機圖。
要求使用WinSock編程,采用其中的TCP面向連接方式,實現文本數據的交換。

代碼片段和文件信息
//?shuangjitongxin.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?
#include?
#include?
#include?
#pragma???comment(lib???“ws2_32.lib“)
using?namespace?std;
int??InputType();
void?ServerProc();
void?ClientProc();
void?TCPSvr();
void?TCPCli();
string?strType;
WSADATA?wsadata;
char?netType;
const?int?defaultPort?=?1005;
const?int?defaultBacklog?=?5;
int?main(int?argc?char*?argv[])
{
????while(InputType()<0);
????if(strType==“s“)
????????ServerProc();
????else?if(strType==“c“)
????????ClientProc();
????else
????????exit(-1);
????while(1)
????{
????};
????return?0;
}
void?ServerProc()
{
????if(WSAStartup(MAKEWORD(20)?&wsadata)!=0){
????????cout<<“WSAStartup?erro\n“;
????????WSACleanup();
????????exit(-1);
????}
????
??
????????cout<<“tcp?Server...\n“;
????????TCPSvr();
????????
}
void?ClientProc()
{
????if(WSAStartup(MAKEWORD(20)?&wsadata)!=0){
????????cout<<“WSAStartup?erro\n“;
????????WSACleanup();
????????exit(-1);
????}
????
????
????????cout<<“tcp?client...\n“;
????????TCPCli();
????????
}
int?InputType()
{
????cout<<“svr?or?cli(s/c):“;
????cin>>strType;
????if(strType==“s“){
????????return?0;
????}
????else?if(strType==“c“){
????????return?1;
????}
????else
????????return?-1;
}
void?TCPSvr()
{
????struct?sockaddr_in?addr?accAddr;?
????int?sock?accSock;
????
????memset(&addr?0??sizeof(addr));
????addr.sin_family?=?AF_INET;
????addr.sin_addr.s_addr?=?htonl(INADDR_ANY);
????addr.sin_port?=?defaultPort;
????if((sock=socket(PF_INET?SOCK_STREAM?IPPROTO_TCP))==INVALID_SOCKET){
????????WSACleanup();
????????exit(-1);
????}
????if(bind(sock?(struct?sockaddr*)&addrsizeof(addr))==SOCKET_ERROR){
????????WSACleanup();
????????exit(-1);
????}
????
????if(listen(sockdefaultBacklog)==SOCKET_ERROR){
????????WSACleanup();
????????exit(-1);
????}
//????cout<<“listening...\n“;
????int?size=sizeof(struct?sockaddr);
????if((accSock?=?accept(sock?(struct?sockaddr*)?&accAddr?&size))!=INVALID_SOCKET){
????????char?FAR*?strAddr?=?inet_ntoa(*((in_addr*)&(accAddr.sin_addr)));
????????cout<<“connected?by?“< ????}
????
????closesocket(accSock);
//????closesocket(sock);
????while(1){
????????if(listen(sockdefaultBacklog)==SOCKET_ERROR){
????????????WSACleanup();
????????????cout<<“listen?error\n“;
????????}
//????????else
//????????????cout<<“listening...\n“;
????????
????????if((accSock?=?accept(sock?(struct?sockaddr*)?&accAddr?&size))==INVALID_SOCKET){
????????????WSACleanup();
????????????cout<<“accpt?error\n“;
????????}
//????????else
//????????????cout<<“accpted...\n“;
????????char*?msg=new?char[500];
????????int?msgLen?=?recv(accSock?msg5000);
????????msg[msgLen]=‘\0‘;
????????if(msgLen>0)
????????????cout< ????????delete[]?msg;
????????msg?=?NULL;
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????540807??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\shuangjitongxin.exe
?????文件?????795940??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\shuangjitongxin.ilk
?????文件?????210205??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\shuangjitongxin.obj
?????文件????5389200??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\shuangjitongxin.pch
?????文件????1106944??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\shuangjitongxin.pdb
?????文件???????2194??2009-06-29?13:47??雙機通信課程設計\shuangjitongxin\Debug\StdAfx.obj
?????文件?????189440??2009-06-29?20:56??雙機通信課程設計\shuangjitongxin\Debug\vc60.idb
?????文件?????290816??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\Debug\vc60.pdb
?????文件???????1262??2009-06-29?13:46??雙機通信課程設計\shuangjitongxin\ReadMe.txt
?????文件???????4910??2009-06-29?20:50??雙機通信課程設計\shuangjitongxin\shuangjitongxin.cpp
?????文件???????3509??2009-06-29?20:56??雙機通信課程設計\shuangjitongxin\shuangjitongxin.dsp
?????文件????????538??2009-06-29?20:57??雙機通信課程設計\shuangjitongxin\shuangjitongxin.dsw
?????文件??????50176??2009-06-29?20:57??雙機通信課程設計\shuangjitongxin\shuangjitongxin.ncb
?????文件??????48640??2009-06-29?20:57??雙機通信課程設計\shuangjitongxin\shuangjitongxin.opt
?????文件????????264??2009-06-29?20:56??雙機通信課程設計\shuangjitongxin\shuangjitongxin.plg
?????文件????????302??2009-06-29?13:46??雙機通信課程設計\shuangjitongxin\StdAfx.cpp
?????文件????????667??2009-06-29?13:46??雙機通信課程設計\shuangjitongxin\StdAfx.h
?????文件?????122368??2010-04-12?21:43??雙機通信課程設計\雙機通信課程設計報告.doc
?????目錄??????????0??2009-06-29?20:53??雙機通信課程設計\shuangjitongxin\Debug
?????目錄??????????0??2009-06-29?20:53??雙機通信課程設計\shuangjitongxin
?????目錄??????????0??2010-04-12?21:43??雙機通信課程設計
-----------?---------??----------?-----??----
??????????????8758182????????????????????21
- 上一篇:最小生成樹圖形化實現
- 下一篇:C語言下CryptoAPI加密系統
評論
共有 條評論