|
Newbie
Join Date: Jun 2005
Posts: 1
Rep Power: 0 
|
Windows sockets programming
Here's the lowdown, I want to create a server program that you can telnet to and it will display whatever the telnet client sent it like this:
#1923: hello
where the #1932 is the file descriptor of the server and the "hello" is the message sent thru telnet.
Basically, I ran into a bit of strangeness in the output.
Here's the code:
/* Server */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#endif
#define ADDR "10.1.1.4"
#define PORT 6900
#define MAXCONN 50
#define BUFFSIZE 10000
int main()
{
int exitflag = 0;
int result;
int len;
int i;
char buffer[BUFFSIZE];
struct sockaddr_in server_address;
struct sockaddr_in client_address;
struct timeval timeout;
fd_set set;
fd_set readset;
#ifdef WIN32
SOCKET fd;
SOCKET client_fd;
WSADATA wsaData;
unsigned long val = 1;
result = WSAStartup(WINSOCK_VERSION, &wsaData);
if(result != NO_ERROR)
printf("Winsock failed to start.\n");
#else
int fd;
int client_fd;
#endif
FD_ZERO(&set);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(fd<0)
{
fprintf(stderr, "Unable to create socket");
abort();
}
/* put the fd into the set. This is the listening set. */
FD_SET(fd, &set);
#ifdef WIN32
result = ioctlsocket(fd, FIONBIO, &val);
#else
result = fcntl(fd, F_SETFL, O_NONBLOCK);
#endif
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl( INADDR_ANY );
server_address.sin_port = htons(PORT);
result = bind(fd, (struct sockaddr*)&server_address, sizeof(server_address));
result = listen(fd, 1);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
#ifdef WIN32
while (1)
{
readset = set;
if( select(FD_SETSIZE, &readset, NULL, NULL, &timeout) > 0)
{
/* Check all sockets */
for(i=0; i<FD_SETSIZE; i++)
{
if(FD_ISSET(set.fd_array[i],&readset))
/* New connection */
if(set.fd_array[i]==fd)
{
len = sizeof(client_address);
client_fd = accept(fd, (struct sockaddr*)&client_address, &len);
FD_SET(client_fd, &set);
recv(client_fd,buffer,BUFFSIZE-1,0);
printf("%d Connected: %s\n", set.fd_array[i], buffer);
send(client_fd, "HEH!!\n", BUFFSIZE, 0);
}
/* Client sent something */
else
{
if( (result = recv(set.fd_array[i],buffer,BUFFSIZE,0) )> 0)
{
if(result > 1)
printf("#%d: %s",set.fd_array[i], buffer);
}
else
{
printf("Closing connection #%d\n", set.fd_array[i]);
closesocket(set.fd_array[i]);
FD_CLR(set.fd_array[i],&set);
}
}
}
}
}
#else
while (1)
{
readset = set;
if( select(FD_SETSIZE, &readset, NULL, NULL, &timeout) > 0)
{
/* Check all sockets */
for(i=0; i<FD_SETSIZE; i++)
{
if(FD_ISSET(i,&readset))
/* New connection */
if(i==fd)
{
len = sizeof(client_address);
client_fd = accept(fd, (struct sockaddr*)&client_address, &len);
FD_SET(client_fd, &set);
recv(client_fd,buffer,BUFFSIZE,0);
printf("%d Connected: %s\n", i, buffer);
send(client_fd, "HEH!!\n", BUFFSIZE, 0);
}
/* Client sent something */
else
{
if( (result = recv(i,buffer,BUFFSIZE,0) )> 0)
{
if(result > 1)
printf("#%d: %s",i, buffer);
}
else
{
printf("Closing connection #%d\n", i);
closesocket(i);
FD_CLR(i,&set);
}
}
}
}
}
#endif
return 0;
}
Sorry bout the number of #directives. I was meaning to make it compilable on unix and windows systems.
Well what happens is that when a client connects to it, it will send a greeting "HEH!" to tell the user of the telnet client that it has connected successfuly. Whats strange is that, not only does the words "HEH!" come out, more junk too comes out. When I type in a messge and press enter in telnet, it doesn't display anything...
Did I miss something? or did I initialize the sockets wrongly? Basically, the output on the telnet window is like this, when I initially connect to the server.:
I was wondering if I overran the buffer or something to get output like that.
|