help regardin c header file..

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.??
 
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 :|
 
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
 
^. 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 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 .
 
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. ?
 
^^ 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
 
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 ..
 
Back
Top