help regardin c header file..

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
alright thanks..and thanks for fixing the code..

forgot to remove the typos.. :S

and is there anyway i can make this work on a non-unix platform :-S?

if there s no other option..can u help me convert this code into a winsock equivalent.??
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
thank you bossssssssssssssssss

i installed cygwin n got the program running

for the client program i got an o/p = "Server wating for new connection" which is some printf command in the client code..

and for the server..

i got o/p = "cant connect to server"

hope thats alright :|
 

sandy453

Active Member
Adept
Nov 21, 2006
233
12
31
whatever you have written as the server code is client code and vice versa. :p

This code represents a simple socket server client mechanism.One of the easiest programs u can write using sockets.:p

Whatever message you typing at the client is sent to the server where it is

recieved and sent back to the client .



Check out the comments to understand the flow of program



Server Code

--------------

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<netdb.h>

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>

#define SRV_TCP_PORT 27000 //port on which the server will be listening

#define MAX_MSG 100

void errExit(char *str)

{

puts(str);

exit(0);

}

int main()

{

int sockfd, newsockfd;

struct sockaddr_in cliadr, srvadr;

int clilen, n;

char mesg[MAX_MSG];

/*initialise the socket library*/

if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)

errExit(" Can't open stream socket ");

/* bind server port */

memset(&srvadr,0,sizeof(srvadr));

srvadr.sin_family = AF_INET;

//allows you to recieve packets irrespective of ip Adress

srvadr.sin_addr.s_addr = htonl(INADDR_ANY);

//this is the port on which socket will be listening for connections

srvadr.sin_port = htons(SRV_TCP_PORT);

/*The bind() function associates a local address with a socket*/

if(bind(sockfd, (struct sockaddr*) &srvadr, sizeof(srvadr))<0)

errExit(" Can't bind local address");

//listen for connections

listen(sockfd,5);

while(1)

{

printf(" Server waiting for new connection : \n");

//if a client tries to connect on the port the server accepts the connection

//clilen = sizeof(cliaddr);

newsockfd = accept(sockfd, (struct sockaddr*) &cliadr, &clilen);

if(newsockfd<0)

errExit(" Accept Error \n");

printf("connected to the client \n");

/* receive segments */

while(1)

{

//recieve the message sent by the client

n = recv(newsockfd,mesg,MAX_MSG,0);

if(n<0)

errExit(" Receive Error \n");

if(n==0)

{

close(newsockfd);

break;

}

//send a message same message to the client

if(send(newsockfd,mesg,n,0) != n)

errExit(" Send Error \n");

printf(" Received ans sent the following message : %s \n", mesg);

} /* while(read_line) */

} /* while(1) */

return 0;

}
Steps to execute

Start the server first

Then the client

o/p in the client

Enter the message to send , Enter # to exit :

this is test-> this is the message you typed .

Received following message : this is test

o/p in the server

Server waiting for new connection :

Received ans sent the following message : this is test
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
^. you ran each prog on a diff comp for this :| ?

and yeah i know it is the basics of socket programming

remember the fact that im just entering 2nd yr and i guess this comes in 4/5th sem :S
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
and yeah.. sry 'bout the wrong naming :S

that was total n00b-dom from me :(
 

sandy453

Active Member
Adept
Nov 21, 2006
233
12
31
v1gnesh said:
^. you ran each prog on a diff comp for this :| ?

and yeah i know it is the basics of socket programming

remember the fact that im just entering 2nd yr and i guess this comes in 4/5th sem :S
same machine .IN_ADR_ANY implies that u dont need to bother abt ip adress.only abt port .

Steps to execute

Start the server first

Then the client .Both on the same machine

Type some message in the client.

The messgae shlud be recied by the server and then server

sends this message back to the client .
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
forgive me for being a total noob but..
i used cygwin wid gcc core...
opened cygwin..
cd ../../ to enter the folder where i stord my server and client .c files
then .
gcc -wall server/client.c
first being server..
after tht i put ./a.exe
thn for server program i get.. "server waiting for new connection "

and.. how do i run client program at the same time. ?
 

sandy453

Active Member
Adept
Nov 21, 2006
233
12
31
gcc server.c -o socket_server //name of the exe

gcc client.c -o socket_client

on a new command prompt start the socket_server

on a new command prompt start the socket_client

repeat the steps which I have given above.

Document on compiling using gcc.Read this

Compiling "C" And "C++" Programs On Unix Systems - gcc/g++

Srry man ,In office right now.Yahoo messengers not allowed .:p
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
ohh k.. thanks for the help.. ill try to figure out the rest..

thanks a lot..
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
im able to run socket succesfully and get.. waitin for connection.

whn im runnin client.. i get.. cant connect to server.. :(
 

Checksum

Well-Known Member
Adept
Aug 3, 2005
1,544
32
112
33
^^ make sure the ports are open in the system. If you are using different computers for the client and server, the ports have to open in the firewall
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
alright.. i figured that out..

thanks :)

and chksum...

i had a stupid mess up wid my cygwin..so im installin it again..

whn im in the package selection part..

i shud select for "gcc core" and "gcc mingw" in the checkboxes. in both the "bin?" and "src?" column??

or ..which 1 shud it b ..
 

v1gnesh

Active Member
Adept
Nov 29, 2006
381
2
29
alright.. thanks for ur help guys..

really appreciate it .. :)

made it work finally :)