1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
try
{
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // TCP
if (sock == INVALID_SOCKET)
{
throw SocketException(WSAGetLastError(), "socket()");
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(2700);
address.sin_addr.S_un.S_addr = ADDR_ANY;
if (::bind(sock, (const struct sockaddr*) &(address), sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
throw SocketException(WSAGetLastError(), "bind()");
}
struct sockaddr_in readdr;
readdr.sin_family = AF_INET;
int ll = sizeof(struct sockaddr_in);
SOCKET remote = ::accept(sock,(sockaddr*)&readdr,&ll);
if(remote == INVALID_SOCKET)
{
throw SocketException(WSAGetLastError(),"accept()");
}
printf("ll: %d",ll);
if (::send(remote, (char*) "Hello wob.", 10, 0) == SOCKET_ERROR) throw SocketException(WSAGetLastError(), "send()");
byte * data[128];
if (::recv(sock, (char*) data, 128, 0) == SOCKET_ERROR)
{
int lerr = WSAGetLastError();
throw SocketException(lerr, "recv()");
}
printf("Received %s\n",data);
closesocket(remote);
closesocket(sock);
} catch (const SocketException &x)
{
print(x.what());
return;
}
|