471,322 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Socket Programming Problem in C

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
2 6578
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
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.

Similar topics

reply views Thread by Rob | last post: by
2 posts views Thread by Jean-Philippe Guyon | last post: by
4 posts views Thread by Ted | last post: by
5 posts views Thread by John Sheppard | last post: by
1 post views Thread by John Sheppard | last post: by
9 posts views Thread by Michael Lindsey | last post: by
4 posts views Thread by Marco Meoni | last post: by
8 posts views Thread by =?Utf-8?B?Sm9obg==?= | last post: by
1 post views Thread by phpuser123 | last post: by

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.