473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ 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_FAILU RE);
}

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,servadd r;
char msg[1024];
int nread;

if((listenfd=so cket(AF_INET,SO CK_STREAM,0))<0 )
err("Socket failed");

memset(&servadd r,0,sizeof(serv addr));
servaddr.sin_fa mily=AF_INET;
servaddr.sin_ad dr.s_addr=htonl (INADDR_ANY);
servaddr.sin_po rt=htons(SERV_P ORT);

if(bind(listenf d,(struct sockaddr*)&serv addr,sizeof(ser vaddr))<0)
err("Bind failed");

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

for(;;)
{
clilen=sizeof(c liaddr);
connfd=accept(l istenfd,(struct sockaddr*)&clia ddr,&clilen);
sendString("New Client");
memset(msg,0,10 24);

while(1)
{
sendString("Per son (server) waiting : \n");
nread=read(conn fd,msg,sizeof(m sg));
msg[nread]=0;
fprintf(stdout, "Message %s",msg);
fflush(stdout);
sendString("Per son (server) Type the mess to be sent :");
fgets(msg,1024, stdin);
write(connfd,ms g,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_FAILU RE);
}

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=sock et(AF_INET,SOCK _STREAM,0))<0)
err("Socket Failed");

memset(&servadd r,0,sizeof(serv addr));

servaddr.sin_fa mily=AF_INET;
servaddr.sin_po rt=htons(SERV_P ORT);
inet_pton(AF_IN ET,argv[1],&servaddr.sin_ addr);

connect(sockfd, (struct sockaddr*)&serv addr,sizeof(ser vaddr));

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

sendString("Per son (Client) waiting...\n");
nread=read(sock fd,msg,sizeof(m sg));
msg[nread]=0;
fprintf(stdout, "%s\n",msg) ;
fflush(stdout);
}while(strlen(m sg)>1);
}

Thanks a lot

Nov 15 '05 #1
2 6963
In article <11************ **********@g44g 2000cwa.googleg roups.com>,
chellappa <N.*********@gm ail.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_FAILU RE);
}

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,servadd r;
char msg[1024];
int nread;

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

memset(&servadd r,0,sizeof(serv addr));
servaddr.sin_fa mily=AF_INET;
servaddr.sin_ad dr.s_addr=htonl (INADDR_ANY);
servaddr.sin_po rt=htons(SERV_P ORT);
htonl and htons do not exist in the C standard.

if(bind(listenf d,(struct sockaddr*)&serv addr,sizeof(ser vaddr))<0)
err("Bind failed");
bind() does not exist in the C standard.

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

for(;;)
{
clilen=sizeof(c liaddr);
connfd=accept(l istenfd,(struct sockaddr*)&clia ddr,&clilen);
accept() does not exist in the C standard.
sendString("New Client");
memset(msg,0,10 24);

while(1)
{
sendString("Per son (server) waiting : \n");
nread=read(conn fd,msg,sizeof(m sg));
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("Per son (server) Type the mess to be sent :");
fgets(msg,1024, stdin);
write(connfd,ms g,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.*********@gm ail.com> wrote:
i did a small Chat program in Linux C Socket Programm ... I am using


(Take it to comp.unix.progr ammer.)

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)cybers pace.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
1390
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 listens using socket.BeginAccept(...). When a client disconnects, the socket is disconnected like this: try {...
2
13722
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 ------ Compiling... cl : Command line warning D4029 : optimization is not available in the
4
4823
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 call to sendto. I was hoping that if someone has the time they could take a look at my code posted below and let me know what i'm doing wrong?
5
3669
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 but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are...
1
3380
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 XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This...
9
8659
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 trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should...
4
3177
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 - codificata per chiarezza, non per efficenza''' def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET,...
8
4666
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 page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data...
1
2579
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 void main(String args) throws Exception {
0
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7455
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7480
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5373
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.