2015-08-01 16:57:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <winsock2.h>
|
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
|
2015-08-02 15:23:26 +00:00
|
|
|
static void dump(unsigned char *buf, unsigned len)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
if ((i % 24) == 0)
|
|
|
|
{
|
|
|
|
printf("\n$%04X:", i);
|
|
|
|
}
|
|
|
|
printf(" %02X", buf[i]);
|
|
|
|
}
|
|
|
|
printf(".\n");
|
|
|
|
}
|
|
|
|
|
2015-08-01 16:57:58 +00:00
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
printf("Init\n");
|
|
|
|
WSADATA wsa;
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-02 15:23:26 +00:00
|
|
|
SOCKET udp = socket(AF_INET, SOCK_DGRAM , IPPROTO_UDP);
|
|
|
|
if (udp == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SOCKET srv = socket(AF_INET, SOCK_STREAM , IPPROTO_TCP);
|
|
|
|
if (srv == INVALID_SOCKET)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long arg = 1;
|
2015-08-02 15:23:26 +00:00
|
|
|
if (ioctlsocket(udp, FIONBIO, &arg) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ioctlsocket(srv, FIONBIO, &arg) == SOCKET_ERROR)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SOCKADDR_IN local;
|
|
|
|
local.sin_family = AF_INET;
|
|
|
|
local.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
local.sin_port = htons(6502);
|
2015-08-02 15:23:26 +00:00
|
|
|
if (bind(udp, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bind(srv, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(srv, 1) == SOCKET_ERROR)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SOCKADDR_IN remote;
|
|
|
|
remote.sin_addr.s_addr = INADDR_NONE;
|
|
|
|
|
2015-08-02 15:23:26 +00:00
|
|
|
SOCKET tcp = INVALID_SOCKET;
|
|
|
|
|
2015-08-01 16:57:58 +00:00
|
|
|
printf("(S)end or e(X)it\n");
|
|
|
|
char key;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
unsigned char buf[1500];
|
|
|
|
|
|
|
|
if (kbhit())
|
|
|
|
{
|
|
|
|
key = getch();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == 's')
|
|
|
|
{
|
2015-08-02 15:23:26 +00:00
|
|
|
if (remote.sin_addr.s_addr == INADDR_NONE && tcp == INVALID_SOCKET)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
2015-08-02 15:23:26 +00:00
|
|
|
printf("Peer Unknown As Yet\n", len);
|
2015-08-01 16:57:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2015-08-11 12:18:52 +00:00
|
|
|
len = 500;
|
2015-08-01 16:57:58 +00:00
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
buf[i] = i;
|
|
|
|
}
|
2015-08-02 15:23:26 +00:00
|
|
|
if (tcp == INVALID_SOCKET)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
2015-08-11 12:18:52 +00:00
|
|
|
printf("Send Len %d To %s", len, inet_ntoa(remote.sin_addr));
|
2015-08-02 15:23:26 +00:00
|
|
|
if (sendto(udp, buf, len, 0, (SOCKADDR *)&remote, sizeof(remote)) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-11 12:18:52 +00:00
|
|
|
printf("Send Len %d", len);
|
2015-08-02 15:23:26 +00:00
|
|
|
if (send(tcp, buf, len, 0) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-01 16:57:58 +00:00
|
|
|
}
|
|
|
|
printf(".\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned remote_size = sizeof(remote);
|
2015-08-02 15:23:26 +00:00
|
|
|
len = recvfrom(udp, buf, sizeof(buf), 0, (SOCKADDR *)&remote, &remote_size);
|
2015-08-01 16:57:58 +00:00
|
|
|
if (len == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 15:23:26 +00:00
|
|
|
else if (len)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
2015-08-11 12:18:52 +00:00
|
|
|
printf("Recv Len %d From %s", len, inet_ntoa(remote.sin_addr));
|
2015-08-02 15:23:26 +00:00
|
|
|
dump(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcp == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
SOCKADDR_IN conn;
|
|
|
|
unsigned conn_size = sizeof(conn);
|
|
|
|
tcp = accept(srv, (SOCKADDR *)&conn, &conn_size);
|
|
|
|
if (tcp == INVALID_SOCKET)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
2015-08-02 15:23:26 +00:00
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
2015-08-01 16:57:58 +00:00
|
|
|
{
|
2015-08-02 15:23:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Connect From %s\n", inet_ntoa(conn.sin_addr));
|
|
|
|
|
|
|
|
u_long arg = 1;
|
|
|
|
if (ioctlsocket(tcp, FIONBIO, &arg) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
return;
|
2015-08-01 16:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 15:23:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
len = recv(tcp, buf, sizeof(buf), 0);
|
|
|
|
if (len == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (len)
|
|
|
|
{
|
2015-08-11 12:18:52 +00:00
|
|
|
printf("Recv Len %d", len);
|
2015-08-02 15:23:26 +00:00
|
|
|
dump(buf, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Disconnect\n");
|
|
|
|
closesocket(tcp);
|
|
|
|
tcp = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sleep(10);
|
2015-08-01 16:57:58 +00:00
|
|
|
}
|
|
|
|
while (key != 'x');
|
|
|
|
|
2015-08-02 15:23:26 +00:00
|
|
|
closesocket(udp);
|
|
|
|
closesocket(tcp);
|
|
|
|
closesocket(srv);
|
2015-08-01 16:57:58 +00:00
|
|
|
WSACleanup();
|
|
|
|
printf("Done\n");
|
|
|
|
}
|