win32 socket编程帮助
2012-06-19 22:46
134 查看
http://msdn.microsoft.com/en-us/library/ms741394(v=vs.85)
client:
Socket programming in MFC is usually a bit messy. It's always clean and convenient to write the socket programs using Win32.
A big advantage writing these programs in Win32 is, we can follow the same model as in any Unix C++ socket program. Also, with each and every new version of Winsock, we don't know how many bugs are going to get added to the development frameworks.
Anyway, now let's get down to business. This article does not try to cover all the minute details of winsock client programming, but for the basics. The relevant steps for creating a synchronous client socket are:
Create a socket and get the handle
Populate the SOCKADDR_IN structure with the Server IP and Port Number
Connect using the connect function
Send and Recv using the socket
Shutdown and Close when all jobs are done
The following sample illustrates the above steps. Only one item missed out here is the usage of recv function.
#include <winsock2.h>
#include <iostream.h>
int gPort = 8780;
void main()
{
SOCKET lhSocket;
SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}
memset(&lSockAddr,0, sizeof(lSockAddr));
lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("IPAddress");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
return;
}
lLength = send(lhSocket,lData,strlen(lData),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhSocket);
return;
}
Note:
The socket programs in MFC need the library ws2_32.lib to be referenced before linking. Otherwise the VC++ linker throws errors.
client:
Socket programming in MFC is usually a bit messy. It's always clean and convenient to write the socket programs using Win32.
A big advantage writing these programs in Win32 is, we can follow the same model as in any Unix C++ socket program. Also, with each and every new version of Winsock, we don't know how many bugs are going to get added to the development frameworks.
Anyway, now let's get down to business. This article does not try to cover all the minute details of winsock client programming, but for the basics. The relevant steps for creating a synchronous client socket are:
Create a socket and get the handle
Populate the SOCKADDR_IN structure with the Server IP and Port Number
Connect using the connect function
Send and Recv using the socket
Shutdown and Close when all jobs are done
The following sample illustrates the above steps. Only one item missed out here is the usage of recv function.
#include <winsock2.h>
#include <iostream.h>
int gPort = 8780;
void main()
{
SOCKET lhSocket;
SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}
memset(&lSockAddr,0, sizeof(lSockAddr));
lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("IPAddress");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
return;
}
lLength = send(lhSocket,lData,strlen(lData),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhSocket);
return;
}
Note:
The socket programs in MFC need the library ws2_32.lib to be referenced before linking. Otherwise the VC++ linker throws errors.
相关文章推荐
- win32 socket编程帮助
- win32 socket编程帮助(二)
- win32 socket编程帮助(二)
- 基于win32的socket编程及程序实现
- 多种语言socket编程集锦—win32
- 【转】Win32下的socket编程
- win32下Socket编程(1)
- Android的Socket编程尝试失败了,求帮助
- C# socket编程中即可以处理Win32Exception也可以处理Exception
- win32 socket编程 示例(功能简单,完善)
- Win32 Socket 通信 WinSock 编程示例 更新
- win32系统托盘图标,对vc编程很有帮助
- Win32 Socket编程注意事项
- socket编程之select()-win32下
- win32 socket编程 示例(功能简单,完善)
- WIN32 socket编程
- [网络编程]Win32 Socket+OpenSSL实例
- win32下Socket编程(1)
- win32下Socket编程(1)
- 多种语言socket编程集锦—win32