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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <SDL.h>
#include <SDL_net.h>
#include <stdlib.h>
#include "defines.h"
#include "messageout.h"
int main(int argc, char *argv[])
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
printf("SDL_Init: %s\n", SDL_GetError());
exit(1);
}
// Set SDL to quit on exit
atexit(SDL_Quit);
// Initialize SDL_net
if (SDLNet_Init() == -1) {
printf("SDLNet_Init: %s\n", SDLNet_GetError());
exit(2);
}
// Try to connect to server
IPaddress ip;
TCPsocket tcpsock;
if (SDLNet_ResolveHost(&ip, "localhost", 9601) == -1) {
printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
exit(1);
}
tcpsock = SDLNet_TCP_Open(&ip);
if (!tcpsock) {
printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
exit(2);
}
printf("Succesfully connected!\n");
// Send a login message
MessageOut msg;
// Register
/*
msg.writeShort(CMSG_REGISTER);
msg.writeString("test");
msg.writeString("password");
msg.writeString("test@email.addr");
*/
// Login
///*
msg.writeShort(CMSG_LOGIN);
msg.writeString("test");
msg.writeString("password");
//*/
// Chat
/*
msg.writeShort(CMSG_SAY);
msg.writeString("Hello World!");
msg.writeShort(0);
*/
// message hex
///*
for (unsigned int i = 0; i < msg.getPacket()->length; i++) {
printf("%x ", msg.getPacket()->data[i]);
}
printf("\n");
//*/
SDLNet_TCP_Send(tcpsock, msg.getPacket()->data, msg.getPacket()->length);
char data[1024];
int recvLength = SDLNet_TCP_Recv(tcpsock, data, 1024);
printf("Received:\n");
if (recvLength != -1)
for (unsigned int i = 0; i < recvLength; i++) {
printf("%x ", data[i]);
}
else
printf("ERROR!");
printf("\n");
SDLNet_TCP_Close(tcpsock);
return 0;
}
|