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 2 6866
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
-...
|
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...
|
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...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |