Socket Programming Problem in C


C

chellappa

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
 
Ad

Advertisements

W

Walter Roberson

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top