Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2005, 6:59 AM   #1
davidsiaw
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 davidsiaw is on a distinguished road
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.
davidsiaw is offline   Reply With Quote
Old Jun 15th, 2005, 7:17 AM   #2
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
Ye got ta NULL terminate your buffer!
__________________

Free code: http://sol-biotech.com/code/.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Mitakeet

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
mitakeet is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC