hi.. i'm very new in this programming thing but i need to make this hello world program work in windows and linux environment. thing is i encoded the program below and i used cygwin and andlinux to compile and i still get i think some errors.

I'm lost.. really i need help.. if anyone can kindly point out to me what is wrong with this codes please explain to me as you will explain to a grade I pupil. thanks..

what am i doing wrong?? should i input some more codes like for examples in the part where this comment appeared?

/* turn off bind address checking, and allow port numbers to be reused otherwise the TIME_WAIT phenomenon will prevent binding to these address.port combinations for (2*MSL) seconds. */

pleasssseeeee...


here's what i've encoded:

#include <stdio.h>
#include <stdlib.h>			/* exit()	*/
#include <string.h>			/* memset (), memcpy()*/
#include <sys/utsname.h>	/* uname()*/
#include <sys/types.h>		
#include <sys/socket.h>		/* socket(), bind(), listen(),accept()*/
#include <netinet/in.h>		
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>			/* fork(), write(), close()*/

/* prototypes*/

int_GetHostName(char  *buffer, int length);

/* contants*/

const char MESSAGE[] = "Hello, World!\n";
const int BACK_LOG=5;

int main(int argc, char *argv[]){
	int serverSocket=0, on=0, port=0,status=0,childPid=0;
	struct hostent *hostPtr=NULL;
	char hostname[80]="";
	struct sockaddr_in serverName={0};

	if (2!=argc){
		fprintf(stderr,"Usage: %s <port>\n",argv[0]);
	}
	port=atoi(argv[1]);
	serverSocket=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(-1==serverSocket){
		perror("socket()");
		exit;
	}

	/*	turn off bind address checking, and allow port numbers to be reused - 
		otherwise the TIME_WAIT phenomenon will prevent binding to these address.port
		combinations for (2*MSL) seconds. 
	*/


	on=1;
	status=setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on,sizeof(on));

	if(-1==status){
		perror("setsockopt(...,SO_REUSEADDR,...)");
	}

	/*	when connection is closed, thereis a need to linger to ensure all data is transmitted, 
		so turn this on also
	*/

	{
		struct linger linger={0};
		linger.1_onoff=1;
		linger.1_linger=30;
		status=setsockopt(serverSocket,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
		if(-1==status){
			perror("setsockopt(...,SO_LINGER,...)");
		}
	}


	// FIND OUT WHO I AM

	status=_GetHostName(hostname,sizeof(hostname));
	if(-1==status){
		perror("_GetHostName()");
		exit(1);
	}

	hostPtr=gethostbyname(hostname);
	if(NULL==hostPtr){
		perror("gethostbyname()");
		exit(1);
	}

	(void)memset(&serverName,0,sizeof(serverName));
	(void)memcpy(&serverName.sin_addr,hostPtr->h_addr,hostPtr->h_length);

	
	/*	to allow server be contactable on any of its IP addresses, uncomnment the following line of code
		

		serverName.sin_addr=hton1(INADDR_ANY);
	
	*/

		
	serverName.sin_family=AF_INET;
	serverName.sin_port=htons(port); //network order
	status=bind(serverSocket,(structsockaddr*)&serverName,sizeof(serverName));
	if(-1==status){
		perror("bind()");
		exit(1);
	}

	status=listen(serverSocket,BACK_LOG);
	if(-1==status){
		perror("listen()");
		exit(1);
	}

	for(;;){
		struct sockaddr_in clientName={0};
		int slaveSocket, clientLength=sizeof(clientName);
		
		(void)memset(&clientName,0,sizeof(clientName));

		slaveSocket=accept(serverSocket,(struct sockaddr*)&clientName,&clientLength);
		if(-1==slaveSocket){
			perror("accept()");
			exit(1);
		}

		childPid=fork();

		switch(childPid){

		case -1: /* ERROR */
					perror("fork()");
					exit(1);
		case 0: /* child process */
					close(serverSocket);
					if(-1==getpeername(slaveSocket,(struct sockaddr*)&clientName,&clientLength)){
						perror("getpeername()");
					}else{
						printf("Connectionrequest from %s \n"; , inet_ntoa(clientName.sin_addr));
					}

					
					/*	Server application spoecific code goes here,
						

						e.g.perform some action, respond to client etc.

					*/

			
					write(slaveSocket,MESSAGE,strlen(MESSAGE));
					close(slaveSocket);
					exit(0);
		default: /*parentprocess*/
			close(slaveSocket);
		}
	}

	return 0;

	}

	/* Local replacement of gethostname() to aid portability */


	int _GetHostName(char *buffer, int length){
		struct utsname sysname={0};
		int status=0;

		status=uname(&sysname);
		if(-1!=status){
			strncpy(buffer,sysname.nodename,length);
		}
		return(status);
	}

Recommended Answers

All 4 Replies

Please wrap your code in code tags with:

and a closing tag [/ code] (without the space)

and also, being new to programming, why would you start on networking? Why not try something simpler.[code=cplusplus]
and a closing tag
[/ code] (without the space)


and also, being new to programming, why would you start on networking? Why not try something simpler.

i got some background in turbo c but it's very elementary.

if you can please help me?

I would help you but you see. I'm only a beginner to Network Programming myself. I still have failed to find a good tutorial. When i try to compile it on DevC++ None of the headers are recognized. What compiler are you using, and in what operating system?

i compiled that using visual c++, but my libraries are not that complete and some of the headers are a native of linux as they said. i'm also using cygwin and andlinux but still posts problems for me.

its suppose to run in windows and linux.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.