LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2004, 01:10 AM   #1
mcp_achindra
LQ Newbie
 
Registered: Mar 2004
Location: Makhanlal Chaturvedi University, Bhopal, INDIA
Posts: 11

Rep: Reputation: 0
Unhappy My C Chat Program is unable to communicate.


I have written a code in c to work on Linux. I m looking to write a program to chat, but being a newbie in this field, I am facing problems. Please help me with that. All I want to design is a wall like command. A server to listen for connections, and echos back the message to all the connected clients. The most gruesome prob;lem I m facing is to make my server listen for multiple connections through a single port, because I can't ask all the clients to connect to different ports. Help me with some working code, I can learn from. If any one could give me the code to simply accept the connection and echo back whatever a client has sent and the welcome message. To the best if any one can help me use the same port for multiple connecions.


Server Code ......

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define DestIP "127.0.0.1"
#define SourcePort 12004

char *msg = "Hello World !";

main ()
{
int sockfd;
struct sockaddr_in source_addr;

if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
printf ("Cannot create socket !\n");
exit (-1);
}

source_addr.sin_family = AF_INET;
source_addr.sin_port = htons(SourcePort);
source_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset (&(source_addr.sin_zero), '\0', 8);

if (bind (sockfd, (struct sockaddr *) &source_addr, sizeof(struct sockaddr))== -1)
{
printf ("Unable to bind connection to the OS.");
exit(-1);
}

for (;
{
msg = "";
recv (sockfd, msg, sizeof (msg), 0);
printf ("%s", msg);
}
}


________________________________________________________________________


Client Code:......


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define DestIP "127.0.0.1"
#define DestPort 12004

char *msg="Hello World !";

main()
{
int sockfd;
struct sockaddr_in dest_addr;

if (sockfd=socket(AF_INET,SOCK_STREAM,0)==-1)
{
printf("Cannot create socket !\n");
exit(-1);
}

dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(DestPort);
dest_addr.sin_addr.s_addr=inet_addr(DestIP);
memset(&(dest_addr.sin_zero),'\0',8);

if ((connect(sockfd,(struct sockaddr *)&dest_addr, sizeof(struct sockaddr)))==-1)
{
printf("Unable to connect to remote host !");
exit(-1);
}

if (send(sockfd, msg, sizeof(msg),0)==-1)
{
printf("Error sending data...");
exit(-1);
}

}



________________________________________________________________________



Please help me write a better code.
 
Old 03-20-2004, 10:04 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I didn't look too closely at your code, but for what you want you only need to create a single listen socket on a single port. Each accept() you do returns a new socket that you use for the communication. The listen socket remains, and you can do further accepts on it.

As a general idea of how I would structure your program, here's a bit of pseudo code for you.

Code:
int main()
{
       int listenSock = CreateListenSocket(4444); // made-up function to create and bind a listen socket
       int clientSock;
       SocketList clientList; // made-up object to store list of sockets

       while(1)
       {
             if (PendingAccept(listenSock))
             {
                  clientSock = accept(listenSock);
                  clientList.Add(clientSock);
             }

             for (all clients in list)
             {
                 int client = GetClientFromListSomehow();
                 if (CanRecv(client))
                 {
                      int bytesRecvd = recv(client);

                      if (bytesRecvd == 0) // client disconnected
                      {
                           clientList.Remove(client);
                      }
                      else
                      {
                           for (each client in client list)
                           {
                                send(receivedText);
                           }
                      }
                 }
             }
       }
}
To check if there are pending accepts, or data that can be read from a socket, you can use the fd_set stuff. Checking if a listen socket can "receive" without blocking is how you check if you can successfully accept a socket connection.

Hope that helps. As I said, it's pseudo code, so you have to fill in a lot of the details, but hopefully it gives you an idea of how you can structure your code.

Edit: Looked at the code a bit more. You are missing 2 major steps in your server code.
1. You must set the socket to a listen state with the listen function.
2. You must accept a connection with the accept function.

Last edited by deiussum; 03-20-2004 at 10:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to communicate using serial port (COM1) /dev/ttys0 vinaybms Linux - Networking 1 03-11-2005 08:08 AM
Chat Program derick.schmidt Linux - Software 4 11-13-2004 07:19 AM
program to communicate directly with ethernet sabby Programming 4 12-18-2002 11:37 AM
gaim - unable to chat because protocols not installed mtint Linux - Software 3 10-20-2002 02:46 PM
Networks unable to communicate with each other hnguy Linux - Networking 6 10-14-2002 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration