By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

wrong in Socket program

P: n/a
Hi,
This is program for socket program for server and client ... but data
send from client and receive by server but i can t sen d message fro m
server to client ... check i mistake .. i treed .. its have some
mistake.. if possible correct and send me

server.c
==========
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define SERVERPORT 1500
#define BUFFERLEN 250

int main(void)
{
int sd,sd2;
int rc,length,on=1;
char buffer[BUFFERLEN];
fd_set read_fd;
struct timeval timeout;
struct sockaddr_in serveraddr;
do
{
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd <0)
{
perror("Socket Failed");
break;
}
rc=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
if (rc <0 )
{
perror(" SOCKEET OPTION FAILED");
break;
}
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(SERVERPORT);

rc= bind(sd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if (rc <0)
{
perror("BIND PLEM");
break;
}
rc=listen(sd,10);
if(rc<0)
{
perror("Listem Plem");
break;
}
printf("Server Ready\n");
sd2=accept(sd,NULL,NULL);
if(sd2<0)
{
perror("accpet failed");
break;
}
length=BUFFERLEN;
rc=setsockopt(sd2,SOL_SOCKET,SO_RCVLOWAT,(char
*)&length,sizeof(length));
if (rc < 0)
{
perror("SD2 OPT FAIL");

}
rc= recv(sd2,buffer,sizeof(buffer),0);
if ( rc <0)
{
perror("recv() failed");
break;
}
printf("%d bytes of data were received %s \n",rc,buffer);
if ( rc == 0 || rc < sizeof(buffer))
{
printf(" The client closed the connection before all of \n");
printf("data was sent\n");
break;
}
strcpy(buffer,"SREE RAM");
rc=send(sd2,buffer,sizeof(buffer),0);
printf ("DAta Sented");
}
while(0);
close(sd);
close(sd2);
}


Client.c
=========

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>

#define SERVER_PORT 1500
#define BUFFER_LENGTH 250
#define FALSE 0
#define SERVER_NAME "ServerHostName"

int main(int argc,char *argv[])
{
int sd,rc,bytesReceived;
char buffer[BUFFER_LENGTH];
char *server=(char *)malloc(sizeof(char *));
struct sockaddr_in serveraddr;
struct hostent *hostp;

do {
sd=socket(AF_INET,SOCK_STREAM,0);
if (sd < 0){
perror("socket() failed");
break;
}

if (argc >1)
strcpy(server,argv[1]);
else
strcpy(server,SERVER_NAME);

memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVER_PORT);
serveraddr.sin_addr.s_addr = inet_addr(server);
if (serveraddr.sin_addr.s_addr == (unsigned long) INADDR_NONE)
{
hostp=gethostbyname(server);
if(hostp == (struct hostent *) NULL)
{
printf("Host Not Found ");
printf("h_errno = %d\n",h_errno);
break;
}
memcpy(&serveraddr.sin_addr,hostp->h_addr,sizeof(serveraddr.sin_addr));
}
else
{
printf ("INADDR FAIL");
}
rc=connect(sd, (struct sockaddr *) &serveraddr , sizeof(serveraddr));
if (rc < 0)
{
perror("connect() failed");
break;
}

memset(buffer, 's',sizeof(buffer));
rc=send(sd,buffer,sizeof(buffer),0);
printf("Data Sended");

if ( rc < 0)
{
perror("send () failed");
break;
}
bytesReceived = 0;
while (bytesReceived < BUFFER_LENGTH);
{
rc= recv(sd, &buffer[bytesReceived],BUFFER_LENGTH -
bytesReceived,0);
printf("Data Recvived");
if ( rc < 0)
{
perror("recv() failed");
break;
}
else if (rc == 0)
{
printf("The Server Closed the connection\n");
break;
}
printf("%c",buffer[bytesReceived]);
bytesReceived += rc;
}

} while(FALSE);

if (sd!=-1)
close(sd);

}
Thanks advance

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

chellappa wrote:
Hi,
This is program for socket program for server and client ... but data
send from client and receive by server but i can t sen d message fro m
server to client ... check i mistake .. i treed .. its have some
mistake.. if possible correct and send me


Troll? Fool? Slow learner?

-David

Nov 15 '05 #2

P: n/a
In article <11**********************@g44g2000cwa.googlegroups .com>,
David Resnick <ln********@gmail.com> wrote:

chellappa wrote:
Hi,
This is program for socket program for server and client ... but data
send from client and receive by server but i can t sen d message fro m
server to client ... check i mistake .. i treed .. its have some
mistake.. if possible correct and send me


Troll? Fool? Slow learner?

-David


Yup.

Nov 15 '05 #3

P: n/a
David Resnick wrote:
chellappa wrote:
Hi,
This is program for socket program for server and client ... but data
send from client and receive by server but i can t sen d message fro m
server to client ... check i mistake .. i treed .. its have some
mistake.. if possible correct and send me

Troll? Fool? Slow learner?

-David


Around here we call them lunch or food ;)

-Joe
Nov 15 '05 #4

P: n/a
"chellappa" <N.*********@gmail.com> writes:
This is program for socket program for server and client ... but data
send from client and receive by server but i can t sen d message fro m
server to client ... check i mistake .. i treed .. its have some
mistake.. if possible correct and send me


Chellappa, you have already been told that sockets are off-topic in
this newsgroup. Try a newsgroup that's specific to the system you're
using, perhaps comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.