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

Socket Programming Problem in C

P: n/a
Hi Every body!,
i did a small Chat program in Linux C Socket Programm ... I am using
stdout/stdin i did in single machinee i will work properly..but i want
to run in differnet machine..i tried ,but its faliure... try to modify
this pgm run in differnt machine

Server Program
==============
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>

#define LISTENQ 100
#define SERV_PORT 9292

void err(char* str)
{
fprintf(stdout,str);
fflush(stdout);
exit(EXIT_FAILURE);
}

void sendString(char* str)
{
fprintf(stdout,str);
fflush(stdout);
}

int main(int argc,char** argv)
{
int listenfd,connfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
char msg[1024];
int nread;

if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
err("Socket failed");

memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);

if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
err("Bind failed");

if(listen(listenfd,LISTENQ)<0)
err("Listen failed");

for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen);
sendString("New Client");
memset(msg,0,1024);

while(1)
{
sendString("Person (server) waiting : \n");
nread=read(connfd,msg,sizeof(msg));
msg[nread]=0;
fprintf(stdout,"Message %s",msg);
fflush(stdout);
sendString("Person (server) Type the mess to be sent :");
fgets(msg,1024,stdin);
write(connfd,msg,strlen(msg));
}
close(connfd);
}

}
Client Program
===========-===
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>

#define SERV_PORT 9292

void err(char* str)
{
fprintf(stdout,str);
fflush(stdout);
exit(EXIT_FAILURE);
}

void sendString(char* str)
{
fprintf(stdout,str);
fflush(stdout);
}

int main(int argc,char** argv)
{
int sockfd;
struct sockaddr_in servaddr;
char msg[1024];
int nread;

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
err("Socket Failed");

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

servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));

do
{
sendString("Person (Client) Enter Message to be sent:");
fgets(msg,1024,stdin);
write(sockfd,msg,strlen(msg));

sendString("Person (Client) waiting...\n");
nread=read(sockfd,msg,sizeof(msg));
msg[nread]=0;
fprintf(stdout,"%s\n",msg);
fflush(stdout);
}while(strlen(msg)>1);
}

Thanks a lot

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


P: n/a
In article <11**********************@g44g2000cwa.googlegroups .com>,
chellappa <N.*********@gmail.com> wrote:
i did a small Chat program in Linux C Socket Programm ... I am using
stdout/stdin i did in single machinee i will work properly..but i want
to run in differnet machine..i tried ,but its faliure... try to modify
this pgm run in differnt machine
Socket Programming and Linux are not considered appropriate topics
for this newsgroup.

Server Program
==============
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
sys/socket.h and arpa/inet.h are not defined by the C standard.
#include <stdlib.h>

#define LISTENQ 100
#define SERV_PORT 9292

void err(char* str)
{
fprintf(stdout,str);
fflush(stdout);
exit(EXIT_FAILURE);
}

void sendString(char* str)
{
fprintf(stdout,str);
fflush(stdout);
}

int main(int argc,char** argv)
{
int listenfd,connfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
char msg[1024];
int nread;

if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
There is no socket() function in the C standard.
err("Socket failed");

memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
htonl and htons do not exist in the C standard.

if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
err("Bind failed");
bind() does not exist in the C standard.

if(listen(listenfd,LISTENQ)<0)
err("Listen failed");
listen() does not exist in the C standard.

for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen);
accept() does not exist in the C standard.
sendString("New Client");
memset(msg,0,1024);

while(1)
{
sendString("Person (server) waiting : \n");
nread=read(connfd,msg,sizeof(msg));
read() does not exist in the C standard. The closest is fread()
which takes a FILE* as its first parameter, not an int.
msg[nread]=0;
fprintf(stdout,"Message %s",msg);
fflush(stdout);
sendString("Person (server) Type the mess to be sent :");
fgets(msg,1024,stdin);
write(connfd,msg,strlen(msg));
write() does not exist in the C standard. The closest is fwrite(),
which expects a FILE* as its final parameter.

}
close(connfd);
close() does not exist in the C standard. The closest is fclose()
which expects a FILE* as its parameter.
}

}

--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 15 '05 #2

P: n/a
chellappa <N.*********@gmail.com> wrote:
i did a small Chat program in Linux C Socket Programm ... I am using


(Take it to comp.unix.programmer.)

Your post is off-topic for comp.lang.c. Please visit

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

for posting guidelines and frequently asked questions. Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.