1
/* network.c - network for typespeed
3
thanks goto Steinar H. Gunderson <sgunderson@bigfoot.com>
4
for the basic functions which we got from him
18
int init_network(char *serv,int graph)
24
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
27
if (graph) endcursestuff();
34
s.sin_family = AF_INET;
35
s.sin_addr.s_addr = inet_addr(serv);
36
s.sin_port = htons(opt.port);
38
if (s.sin_addr.s_addr == -1) {
42
mvprintw(10,30,"Looking up %s...",serv);
45
printf("Looking up %s...\n", serv);
47
he = gethostbyname(serv);
50
if (graph) endcursestuff();
54
bcopy(he->h_addr, (char *) &s.sin_addr, he->h_length);
58
mvprintw(10,30,"Connecting to typespeed server at %s (port %d)...\n",
59
inet_ntoa(s.sin_addr),opt.port);
62
printf("Connecting to typespeed server at %s (port %d)...\n",
63
inet_ntoa(s.sin_addr),opt.port);
66
if (connect(sock, (struct sockaddr *) &s, sizeof(s)) == -1) {
67
if (graph) endcursestuff();
71
i=net_ident(sock,0,graph);
76
struct sockaddr_in addr;
79
bzero((char *) &addr, sizeof(addr));
81
addr.sin_family = AF_INET;
82
addr.sin_addr.s_addr = INADDR_ANY;
83
addr.sin_port = htons(opt.port);
87
ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
89
if (graph) endcursestuff();
93
setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
94
if (bind(ss, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1) {
95
if (graph) endcursestuff();
96
perror("cannot bind to port");
100
if (listen(ss, 1) == -1) {
101
if (graph) endcursestuff();
107
mvprintw(10,30,"Waiting for connections...");
110
printf("Waiting for connections...\n");
113
sock = accept(ss, INADDR_ANY, NULL);
116
if (graph) endcursestuff();
122
i=net_ident(sock,ss,graph);
123
if(i!=0) {close(ss); return i;}
128
ioctl(sock, FIONBIO, &one);
134
int net_ident(int typesock,int tss,int graph) {
139
sprintf(buf,"TYPESPEED %s\n",TVERSION);
140
send(typesock, buf, strlen(buf), 0);
145
mvprintw(10,30,"Connect.");
146
mvprintw(10,30,"Waiting for other party to identify...");
149
printf("Connect.\n");
150
printf("Waiting for other party to identify...\n");
153
i = recv(typesock, buf, 59, MSG_PEEK);
154
if (i == -1 && errno != EWOULDBLOCK) {
155
if (graph) endcursestuff();
160
recv(typesock, buf, strlen(buf) + 1, 0);
161
if(strstr(buf,"TYPESPEED") > 0) {
163
mvprintw(10,30,"Remote is using: %s",buf);
166
printf("Remote is using: %s\n",buf);
170
mvprintw(10,30,"Remote is not TYPESPEED");
173
printf("Remote is not TYPESPEED\n");
185
void net_waitgame(int typesock) {
190
sprintf(buf,"TYPESPEED %s\n",TVERSION);
192
i=send(typesock, buf, strlen(buf), 0);
198
i = recv(typesock, buf, 59, MSG_PEEK);
199
if (i == -1 && errno != EWOULDBLOCK) {
205
recv(typesock, buf, strlen(buf) + 1, 0);
206
if(strstr(buf,"TYPESPEED") > 0) {
216
void net_swapscore(int typesock, stats_struct *stat,stats_struct *stat2)
224
sprintf(buf,"SCORE: %d %f %f %f\n",stat->score,stat->speed,stat->totalspeed,stat->ratio);
226
i=send(typesock, buf, strlen(buf), 0);
232
i = recv(typesock, buf, 59, MSG_PEEK);
233
if (i == -1 && errno != EWOULDBLOCK) {
239
recv(typesock, buf, strlen(buf) + 1, 0);
240
if(strstr(buf,"SCORE:") > 0) {
246
number=malloc(80*sizeof(char));
247
temp=malloc(80*sizeof(char));
250
for(x=7;x<=strlen(buf);x++) {
256
strncpy(number,temp,strcspn(temp," "));
257
stat2->score=strtol(number, NULL, 10);
260
temp2=malloc(80*sizeof(char));
263
for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
269
number=malloc(80*sizeof(char));
270
strncpy(number,temp2,strcspn(temp2," "));
271
stat2->speed=(float) atof(number);
273
temp=malloc(80*sizeof(char));
276
for(x=strcspn(temp2," ")+1;x<=strlen(temp2);x++) {
282
number=malloc(80*sizeof(char));
283
strncpy(number,temp,strcspn(temp," "));
284
stat2->totalspeed= (float) atof(number);
285
temp2=malloc(80*sizeof(char));
288
for(x=strcspn(temp," ")+1;x<=strlen(temp);x++) {
293
stat2->ratio= (float) atof(temp2);