MFC socket的使用方法服务器端:
在.h文件中。
1 struct ListeningThreadStruct
2 {
3 CString sSaveDir;
4 int nListenPort;
5 };
6 UINT ListeningThreadProc(LPVOID lparam);
7 struct FileTransferThreadStruct
8 {
9 CString sSaveDir;
10 SOCKET hSOCKET;
11 };
12
13 UINT FileTransferThread(LPVOID lparam);
在.cpp文件中。
1 UINT CSocketFun::ListeningThreadProc(LPVOID lparam)
2 {
3 ListeningThreadStruct *pPara = (ListeningThreadStruct*)lparam;
4 int nListenPort = pPara->nListenPort;
5 CString sSaveDir = pPara->sSaveDir;
6 delete pPara;
7 pPara = NULL;
8
9 SocketThreadInit();
10 try
11 {
12 CSocket liseningSocket;
13 if ( !liseningSocket.Create(nListenPort) )
14 {
15 LOG("创建TCP端口失败, 可能是端口%d被占用", nListenPort);
16 return 1;
17 }
18
19 if ( !liseningSocket.Listen(5) )
20 {
21 LOG("启动Socket监听失败");
22 return 1;
23 }
24
25 while(true)
26 {
27 CSocket clientSocket;
28 if ( liseningSocket.Accept(clientSocket) )
29 {
30 FileTransferThreadStruct *pFileTrans = new FileTransferThreadStruct;
31 pFileTrans->hSOCKET = clientSocket.Detach();
32 pFileTrans->sSaveDir = sSaveDir;
33
34 AfxBeginThread(FileTransferThread2, (LPVOID)pFileTrans);
35 }
36 else
37 {
38 liseningSocket.Close();
39 break;
40 }
41 }
42 }
43 catch()
44 {
45 LOG("监控Socket线程收到异常, 监听结束");
46 return 0;
47 }
48 return 0;
49 }
50
51
52 UINT CSocketFun::FileTransferThread2(LPVOID lparam)
53 {
54 FileTransferThreadStruct *pPara = (FileTransferThreadStruct*)lparam;
55 SOCKET sock = pPara->hSOCKET;
56 CString sSaveDir = pPara->sSaveDir;
57 delete pPara;
58 pPara = NULL;
59
60 SocketThreadInit();
61
62 CSocket aSocket;
63 aSocket.Attach(sock);
64
65 char buffer[SERVER_SOCKET_BUFFERSIZE] = {'\0'};
66 int nLen = aSocket.Receive(buffer, MAX_PATH);
67 } 调用线程.cpp文件中。
1 ListeningThreadStruct *pPara = new ListeningThreadStruct;
2 pPara->sSaveDir = m_sSaveDir;
3 pPara->nListenPort = m_nListenPort;
4
5 CWinThread *pThread = AfxBeginThread(ListeningThreadProc, (LPVOID)pPara);
客户端:
示例代码:
1 #define OutStr(x, ) \
2 {\
3 CString sMsg; \
4 sMsg.Format(x, __VA_ARGS__); \
5 if (sMsg.GetLength() < (int)nErrBufLen) \
6 strcpy_s(pErrorBuf, nErrBufLen, sMsg); \
7 else \
8 strcpy_s(pErrorBuf, nErrBufLen, "缓冲区大小不足"); \
9 }
10
11
12 AFX_MANAGE_STATE(AfxGetStaticModuleState());
13
14 if (!AfxSocketInit())
15 {
16 return 1 ;
17 }
18
19 SocketThreadInit();
20
21 CSocket aSocket;
22 if (!aSocket.Create())
23 {
24 OutStr("创建socket失败");
25 return 1;
26 }
27
28 if (!aSocket.Connect(sServerIP, nPort))
29 {
30 OutStr("连接服务器失败IP %s 端口 %d", sServerIP, nPort);
31 return 1;
32 }
33
34 CFile file;
35 if (!file.Open(sFile, CFile::modeRead))
36 {
37 OutStr("发送文件时, 文件%s打开失败", sFile);
38 return 1;
39 }
40
41 // 1. send file name
42 char fileName[MAX_PATH] = {'\0'};
43 strcpy_s( fileName, MAX_PATH, CCommonFun::GetFileFullName(sFile) );
44 if ( SOCKET_ERROR == aSocket.Send(fileName, MAX_PATH) )
45 {
46 OutStr("文件名通过socket发送失败");
47 return 1;
48 }
49
50 // 2. send file size
51 ULONGLONG ulFileLen = file.GetLength();
52 if (SOCKET_ERROR == aSocket.Send(&ulFileLen, sizeof(ULONGLONG) ) )
53 {
54 OutStr("文件长度通过socket发送失败");
55 return 1;
56 }
57
58 try
59 {
60 char buffer[CLIENT_SOCKET_BUFFERSIZE] = {'\0'};
61 UINT nTotalLen = 0;
62 UINT nLen = 0;
63 while ((nLen = file.Read(buffer, CLIENT_SOCKET_BUFFERSIZE)) > 0)
64 {
65 if (SOCKET_ERROR == aSocket.Send(buffer, nLen))
66 {
67 OutStr("文件数据通过socket发送失败, 已发送%d", nTotalLen);
68 return 1;
69 }
70
71 nTotalLen += nLen;
72 }
73 }
74 catch ()
75 {
76 OutStr("发送文件数据时收到一个异常, 发送失败");
77 return 1;
78 }
79
80 #define SOCKET_OK_BUFFERSIZE 1024
81 char szOK[SOCKET_OK_BUFFERSIZE] = {'\0'};
82 int nLen = aSocket.Receive(szOK, SOCKET_OK_BUFFERSIZE);
83 if (CString(szOK) != "ok")
84 {
85 OutStr("未收到合法的服务器回文, 发送失败");
86 return 1;
87 }
posted on 2012-04-26 13:58
王海光 阅读(1829)
评论(0) 编辑 收藏 引用 所属分类:
MFC