i created a simple server-client application in which simple I/O is sent and received. but apparently, when the client sends a packet and the server receives it, the buffer to which the data is received is injected with random characters, usually caused because of there not being a string termination (\0), but there is.
here's the client:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "Headers\\sock.h"
int main()
{
sock s;
char data[512];
startup();
opensock(&s);
printf("Connection[%d]\n",sockconnect(&s, "127.0.0.1", 43594));
while(1) {
scanf("%s", data);
data[strlen(data)] = '\0';
if (send((SOCKET) s, data, strlen(data), 0) == -1)
break;
else
printf("SENT %s\n", data);
int i;
for (i = 0; i < strlen(data); i++)
data[i] = 0;
}
return 0;
}
here is the server:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Headers\\sock.h"
typedef struct {
sockinf *sin;
int threadnum;
} sockinf_;
void handle(void *s)
{
sockinf_ *sn = (sockinf_ *)s;
char buff[512];
printf("Client[%d]: Connected from %s\n", sn->threadnum, getip(sn->sin->addr));
while(1) {
if (read(&(sn->sin->s), buff, 512) > 0) {
printf("Length %d\n", strlen(buff));
buff[strlen(buff)] = '\0';
printf("Client[%d]: %s\n", sn->threadnum, buff);
int i;
for (i = 0; i < strlen(buff); i++)
buff[i] = 0;
}
Sleep(1000);
}
}
int main()
{
sock s;
printf("%d\n",startup());
printf("%d\n", opensock(&s));
printf("%d\n",sockbind(&s, 43594));
if (start(&s, 100) != -1)
printf("Started server..\n");
int i;
for (i = 1;; i++) {
sockinf_ ss;
sockinf ii = acceptsock(&s);
ss.sin = ⅈ
ss.threadnum = i;
_beginthread(handle, 0, (void *)&ss);
}
}
by the way, it has nothing to do with the 'sock' header i provide since it directly uses the winsock functions and returns the same return code.