473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

wrong in Socket program

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
4 2265

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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
1
by: Laura T. | last post by:
Hi, I've this kind a program, using sockets to communicate with the clients. One of the clients can be a web browser (like IE). When using IE as a client, the transmission blocks completely, and...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
5
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server)...
6
Sagittarius
by: Sagittarius | last post by:
I will first try to describe my problem in words. I have a simple program, written in C++, that needs to send a single bytearray via a UDP socket to a microprocessor, which returns an answer, also...
1
by: Steve Mol | last post by:
I have a program that works perfectly, as long as only one socket is in use at a time. If I open a second socket, the program works great - for a random amount of time. Then, suddenly, my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.