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 192.168.1.1 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