Thread: Chat udp server/client

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005

    TCP/UDP server/client

    Hi, I need some help.
    I am trying to create a very simple chat program using sockets for a project in school but I am stuck.
    Here is what must be done. I must create a TCP server who accepts incoming connections (for simplicity two at a time) on a port supplemented from the command line lets say 9950. When those two clients connect to the server, then the server must send them the information of the other client and close the connection and continue to wait the next two clients or close all together. I have implemented the last.
    The client takes as parameters the IP of the server and the port it listens, lets say and 9950. So in the code of the client I create a SOCK_STREAM socket connect to the server and read() the info of the other client in a struct sockaddr_in remote_addr and close the connection. Up till now all good. Now I am requested to start a conversation between the two clients using SOCK_DGRAM sockets and using the next port I used for the server;that is port 9951. So what I thought is to create a new socket of the SOCK_DGRAM type. But as I understood each client must be a UDP server AND UDP clinet to the other. So I thought to use fork to create a new process and finally to have the parent for instance to have an endless loop where he recvfrom() messages and the child in an endless loop where the user is typing messages and sendto()...
    I have written code up to the fork point. I am not sure how to send the first message but before I post any code I request to tell me if my idea is basically correct or if there is some point I haven't noticed.

    Furthrmore as far as my code goes:
    By the way I test my code by winscp and putty. I upload my files in the linux server and use three putty windows to compile and run the server and the two clients. The reason I report this is because I think it may has something to do with my problem.
    After I fill the remote_addr I suppose I cant use it to send a message to the other client because as I said I make them also a UDP server and he is bind()ed in port 9951 so I must change the sin_port field of remote_addr right? But when I do that I think it sends the messages to itself. More specifically if I use inet_ntop with remote_addr after I change the port it prints the same with local_addr which is the struct I used to bind() the new socket. Anyway before I put code on here I attached some of the code in my files that is relevant to my problem...
    Also if I dont want to use INADDR_ANY how can write code to get the IP of the machine the program is running and fill it in the struct
    Last edited by Phoenix_Rebirth; 11-16-2008 at 03:35 PM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    1. this is a networking-related issue, not general C programming
    2. you're making things terribly complicated, this is much simpler, the server accept() the two connections from the clients, then wait for incoming data from any of the 2 clients (use select(), read the manpage, find examples on the net), read() data, write() it to the other client fd and loop on select(). A client is pretty much similar: connect to the server, wait incoming data from either the server (i.e. from the other client) and print it, or from the input stream (stdin) still using select(), and send it back to the server. I did not understand your explanation about the fork() part, but I doubt you need it, keep it simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  2. Flashdaddee chat server/client combo
    By ygfperson in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-13-2003, 09:05 AM
  3. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  4. Chat server/client trial -- anyone interested?
    By ygfperson in forum A Brief History of
    Replies: 3
    Last Post: 01-23-2003, 10:47 PM
  5. Traceroute using UDP and ICMP
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2002, 07:09 PM