• 0回复贴,共1

VC socket通信例子

只看楼主收藏回复

转自http://topic.csdn.net/t/20021215/18/1262988.html
//client
#include <winsock2.h>
#include <winbase.h>
#include <iostream>
#define PORT 5000
void main(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
{
return;
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return;
}
/*
//get local ip address
char name[32];
int rtnvl_gethostname;
rtnvl_gethostname = gethostname(name, sizeof(name));
if(rtnvl_gethostname == SOCKET_ERROR)
{
std::cout < < "get host name fail ! " < <std::endl;
}
struct hostent FAR * hostent;
hostent = gethostbyname(name);
printf( "ip address is %s\n ", inet_ntoa(*((struct in_addr *)hostent-> h_addr)));
*/
SOCKET sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.S_un.S_addr = inet_addr( "10.96.128.149 ");
int rtnvl_connect;
rtnvl_connect = connect(sock, (struct sockaddr FAR*)&server, sizeof(struct sockaddr_in));
if(rtnvl_connect == SOCKET_ERROR)
{
int erro = WSAGetLastError ();
std::cout < < "connect fail ! " < <std::endl;
Sleep(3000);
closesocket(sock);
WSACleanup();
return ;
}
char buffer[1024];
int count;
count = recv(sock, buffer, sizeof(buffer), MSG_PEEK);
if(count == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
return ;
}
std::cout < <buffer < <std::endl;
Sleep(3000);
closesocket(sock);
WSACleanup();
}
----------------------------------------
//server
#include <winsock2.h>
#include <winbase.h>
#include <iostream>
#include <fstream>
#define PORT 5000
#define MAXLISTEN 5
void main(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
{
return;
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return;
}
SOCKET sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET)
{
std::cout < < "socket fail ! " < <std::endl;
Sleep(3000);
return ;
}
struct sockaddr_in localaddr;
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(PORT);
localaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
int rtnvl_bind;
rtnvl_bind = bind(sock, (struct sockaddr *)&localaddr, sizeof(struct sockaddr_in));
if(rtnvl_bind == SOCKET_ERROR)
{
std::cout < < "bind fail ! " < <std::endl;
Sleep(3000);
return ;
}
int rtnvl_listen;
rtnvl_listen = listen(sock, MAXLISTEN);
if(rtnvl_listen == SOCKET_ERROR)
{
std::cout < < "listen fail ! " < <std::endl;
Sleep(3000);
return ;
}
while(true)
{
SOCKET newsocket;
struct sockaddr_in clientaddr;
int length;
length = sizeof(struct sockaddr_in);
newsocket = accept(sock, (struct sockaddr FAR *)&clientaddr, &length);
if(newsocket == INVALID_SOCKET)
{
std::cout < < "accept fail ! " < <std::endl;
Sleep(3000);
int error = WSAGetLastError();
std::cout < <error < <std::endl;
Sleep(3000);
break ;
}
//send file
char buffer[] = "this is the server ! ";
int rtnvl_send;
rtnvl_send = send(newsocket, buffer, sizeof(buffer), MSG_DONTROUTE);
if(rtnvl_send == SOCKET_ERROR)
{
std::cout < < "send fail ! " < <std::endl;
Sleep(3000);
break ;
}
//
closesocket(newsocket);
}
closesocket(sock);
WSACleanup();
}


IP属地:辽宁1楼2015-03-27 10:02回复