Faking a network communication to test client/server on one computer - c

I have a course project where I'm suppose to present some networking stuff. There are great chances that I have to do it on one computer. So my question is, how do I show a communication between tcp/ip or tcp/udp? I'm not a networking expert, I understand the concept of sending data which is red. And I think I need two IP addresses.

You don't need two IP addresses. Just start the server on the machine, and have the client connect to address 127.0.0.1 and the appropriate port. (This address always refers to the same machine the program is running on.)

You can run a virtual machine on your computer and work on network between them.

Related

which IP to use to connect to two different computers using telnet C

I have two laptops, both running Ubuntu.
I want to write a chat server in C, which can talk to my other laptop and receive messages back from it.
I know that when the client and server programs are both in a single machine, I can use local host (127.0.0.1) and make them talk using telnet but what about connecting two different computers (which are on the same network)? Which IP and port should I use for them?
A is the Server with IP[A] address (ex: 192.168.1.2)
B is the Client with IP[B] address
So on your chat Server on A at some point in the code you will open a port, you will specify a port number to open. If the number of the port is X then on the client you will connect to IP[A]:X
To give you an example with X=4444, you will open a connection like: 192.168.1.2:4444
Regards,
Basically all that you need to make sure as far as a port goes is that the port is not already in use by another service. There is a number a ways to do this, but I suggest you start by looking at commonly used ports.
As far as IP goes, you are going to want to determine if you want to use the public or private IP of the machines that you are attempting to connect. If you are using the public IPs you will probably have to set up port forwarding in some capacity. If you are just connecting two private ips (i.e. the one the same network) then you don't need to port forward.
Ultimately, this issue is highly specific to you use case and we would need more information to answer it for your exact case.

How to run Client-Server on different computers using Sockets in C

I am able to run a client and server on the same computer on two different terminals in Linux. Now I want to run the same model on two different computers. I am guessing there is more to this than just changing the IP address and the port number to that of the Server.
Any guidance on the matter is highly appreciated.
Thanks.
Make sure the port is forwarded in your router and exceptions for the port is added to the firewall. Then you should be ready to go!
If you have the computers in the same network you can do it without forwarding your port.

simulate different PC with different IP addresses in linux environment

im new to linux environment and any help/feedback would be appreciated. Im actually trying to develop a client-server (MULTICAST) program, so, i would like to test one client sending information to different servers (one-to-many relationship). thus, i would like to simulate different server side in linux with different IP addresses in one computer.
Did you try using different ports instead? I didn't try it myself, but perhaps that can help you in the mid-time.
If you're really multicasting, you don't need to worry about physical host-specific IP:s, all you should need to do is make sure all the programs (clients and servers) are using the same multicast group addresses. Then they should all see each other's traffic automatically.
There's nothing stopping you from running multiple clients on the same machine that also runs the server, in this case.
I sounds like you want to test your code with different IP's. You can create IP aliases on your interface and simulate multiple IP's on one computer.
for e.g. if eth0 is you're active interface with IP, say 192.168.5.11 you can assign another IP to eth0:0 (an alias to eth0) as below.
ifconfig eth0:0 192.168.5.12 netmask255.255.255.0 up
ifconfig eth0:1 192.168.5.13 netmask255.255.255.0 up
run your server on one of the IP's and distribute clients to all your aliases
Use either of the following when you do not have sufficient hardware:
Multicast loop which has the IP stack redirect outbound packets to local receivers.
Virtual machines.
Be aware that semantics of the socket option for #1 change depending on the operating system; for #2 only some virtual machines support multicast, refer to the vendor for details.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms739161(v=vs.85).aspx
Ultimately though you must test with different machines due to specific artifacts of how hosts manage multicast groups. You can for instance create send-only membership which will block every other application on the host. Also consider that an internet, lower case 'I', will introduce further artifacts regarding group joining and propagation delays and drops that your application may need to be aware of.
You can create multiple IP for same machine with help of IP alias. As mentioned above.
But to create multiple Server at one PC you must need different port for each server if you want to simulate the all server behavior with network as well.
I mean for one port multicast traffic always goes to that and some process in the PC will be receiving the packet and has to serve for all server in the PC, Means you have one packet only and all server is receiving with locally manipulation.
But really simulation would be you have multiple server at 1 PC and all are receiving multicast traffic from network rather then from local process.
my Solution: You keep number for server == number of port at the PC. Client send the multicast traffic over all port simultaneously and all server at the PC end will be receiving multicast packet from corresponding port from the Network.
Please correct me if my understanding is wrong.

Writing Client/Server Program in C on UNIX Server

I am trying to write a basic C client/server program using sockets in Unix. I am logging on my school's Unix server from my home computer. I am logging on twice, once to simulate the server and the other to simulate the client. Do I use AF_INET or AF_UNIX? Whose IP address should I use, the one from my home computer or my school? If I use my school's IP address, how do I find out what it is? How do I find out what port number I should use? Does the port number for my client and server have to be the same?
Usually you'll want to use AF_INET — then you'll be able to communicate between more than one computer later.
Use 127.0.0.1; that means "this computer".
Make up a port number and use it for both. Usually you'll want to pick something between 1024 and 65536, exclusive.
Since the programs are running on the schools computer you should use that IP address.
You can use the command /usr/sbin/ifconfig -a to find it (it's the inet address). However, if both logins are at the same physical computer (for example at my school that isn't always the case) you can just use 127.0.0.1.
You can use any port number you want, but choose a high (like four digits) since the lower ones are default for some services.
The port numbers for the client and server do not have to be the same, but it might be easier for you to remember if they are.
See here for a lot of examples. There is some stuff you have to understand:
TCP, UDP and the difference between them
What is a socket, types of sockets (stream, datagram, sequential packets)
Socket API's - BSD, POSIX, WinSock (if you're planning to program for Windows)

Detect whether a socket program is connecting to itself

How, in C, can I detect whether a program is connecting to itself.
For example, I've set up a listener on port 1234, then I set up another socket to connect to an arbitrary address on port 1234. I want to detect whether I'm connecting to my own program. Is there any way?
Thanks,
Dave
Linux provides tools that I think can solve this problem. If the connection is to the same machine, you can run
fuser -n tcp <port-number>
and get back a list of processes listening to that port. You can then look in /proc and found out if there is a process with a pid not your own which is running the same binary you are. A bit of chewing gum and baling wire will help keep the whole contraption together.
I don't think you can easily ask questions about a process on another machine.
One of the parameters to the accept() function is a pointer to a struct sockaddr.
When you call accept() on the server side it will fill in the address of the remote machine connecting to your server socket.
If that address matches the address of any of the interfaces on that machine then that indicates that the client is on the same machine as the server.
You could send a sequence of magic packets upon connection, which is calculated in a deterministic way. The trick is how to do this in a way that sender and receiver will always calculate the same packet contents if they are from the same instance of the program. A little more information on what your program is would be helpful here, but most likely you can do some sort of hash on a bunch of program state and come up with something fairly unique to that instance of the program.
I assume you mean not just the same program, but the same instance of it running on the same machine.
Do you care about the case where you're connecting back to yourself via the network (perhaps you have two network cards, or a port-forwarding router, or some unusual routing out on the internet somewhere)?
If not, you could check whether the arbitrary address resolves to loopback (127.0.0.1), or any of the other IP addresses you know are you. I'm not a networking expert, so I may have missed some possibilities.
If you do care about that "indirect loopback" case, do some handshaking including a randomly-generated number which the two endpoints share via memory. I don't know whether there are security concerns in your situation: if so bear in mind that this is almost certainly subject to MITM unless you also secure the connection.

Resources