473,463 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Similar topics

0
by: Rob | last post by:
Hello, I've got a huge problem with async sockets. I've created a networked virtual environment, using async socket programming. The problem I face occurs when I close the listener socket. It...
2
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
4
by: Ted | last post by:
Hi all, I am trying to learn C socket programming and I have a small program which is a UP client. The problem is, when I run the program, I get a runtime error - "Invalid Argument" - from a...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
9
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am...
4
by: Marco Meoni | last post by:
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa -...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
1
by: phpuser123 | last post by:
I just started socket programming in java.I read a few tutorial and came up with the following script import java.net.*; public class socket { /** * @param args */ public static...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.