![]() |
#2
mayuebo2012-02-22 01:16
|

服务进程
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 1;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET sock_listen = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_server;
addr_server.sin_addr.S_un.S_addr = INADDR_ANY;
addr_server.sin_family = AF_INET;
addr_server.sin_port = htons(4000);
int ret = 0;
int error = 0;
ret = bind(sock_listen,(LPSOCKADDR)&addr_server,sizeof(addr_server));
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Bind error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
ret = listen(sock_listen,5);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Listen error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
int sock_len = sizeof(SOCKADDR);
SOCKET sock_client = accept(sock_listen,(LPSOCKADDR)&addr_server,&sock_len);
if (sock_client==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Accept error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
char recvBuf[100]={""};
char tempBuf[200]={""};
ret = recv(sock_client,recvBuf,strlen(recvBuf)+1,0);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Recv error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
sprintf_s(tempBuf,strlen(tempBuf)+1,"%s",recvBuf);
printf("%s\n",recvBuf);
shutdown(sock_listen,0);
closesocket(sock_listen);
WSACleanup();
return 0;
}
客户进程:
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 1;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET sock_client = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_client;
addr_client.sin_addr.S_un.S_addr = inet_addr("172.18.112.239");
addr_client.sin_family = AF_INET;
addr_client.sin_port = htons(4000);
int ret = 0;
int error = 0;
ret = connect(sock_client,(LPSOCKADDR)&addr_client,sizeof(addr_client));
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Connect error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
char sendBuf[100]={"my name is xxxxx"};
ret = send(sock_client,sendBuf,strlen(sendBuf),0);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Send error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
shutdown(sock_client,0);
closesocket(sock_client);
WSACleanup();
return 0;
}
//错误时bind绑定错误,我修改端口了,还是不行,求原因,正在探索中。。。。。。。。。。。。。。。。。。。。。。
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 1;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET sock_listen = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_server;
addr_server.sin_addr.S_un.S_addr = INADDR_ANY;
addr_server.sin_family = AF_INET;
addr_server.sin_port = htons(4000);
int ret = 0;
int error = 0;
ret = bind(sock_listen,(LPSOCKADDR)&addr_server,sizeof(addr_server));
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Bind error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
ret = listen(sock_listen,5);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Listen error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
int sock_len = sizeof(SOCKADDR);
SOCKET sock_client = accept(sock_listen,(LPSOCKADDR)&addr_server,&sock_len);
if (sock_client==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Accept error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
char recvBuf[100]={""};
char tempBuf[200]={""};
ret = recv(sock_client,recvBuf,strlen(recvBuf)+1,0);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Recv error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
sprintf_s(tempBuf,strlen(tempBuf)+1,"%s",recvBuf);
printf("%s\n",recvBuf);
shutdown(sock_listen,0);
closesocket(sock_listen);
WSACleanup();
return 0;
}
客户进程:
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 1;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET sock_client = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_client;
addr_client.sin_addr.S_un.S_addr = inet_addr("172.18.112.239");
addr_client.sin_family = AF_INET;
addr_client.sin_port = htons(4000);
int ret = 0;
int error = 0;
ret = connect(sock_client,(LPSOCKADDR)&addr_client,sizeof(addr_client));
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Connect error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
char sendBuf[100]={"my name is xxxxx"};
ret = send(sock_client,sendBuf,strlen(sendBuf),0);
if (ret==SOCKET_ERROR)
{
#ifdef _DEBUG
printf("Send error:%d\n",(error=WSAGetLastError()));
#endif
return 0;
}
shutdown(sock_client,0);
closesocket(sock_client);
WSACleanup();
return 0;
}
//错误时bind绑定错误,我修改端口了,还是不行,求原因,正在探索中。。。。。。。。。。。。。。。。。。。。。。