I want to exchange messages between client and server which are on two different machines. The two machines are directly connected by an Ethernet cable. I can successfully send and receive messages between client and server if they are both on the same machine. What should be the server address in order to have communication between the two machines?
serveraddr.sin_addr.s_addr = htonl( ???? );
I've tried running ifconfig on the server machine but just found the MAC address and no IP to assign in the code.
Both machines are running DHCP.
Have a look at the UDP wiki, as you can see, it requires an network layer. This is typically IP. You can set a static IP address to both devices and use these static IP address or you can set up a DHCP server on one of these machines to assign IP addresses automatically.
Directly connecting the machines with a cable creates a unique independent network (with only the two nodes).
Note that the cable will have to be a crossover cable.
DHCP probably won't help if neither node is a DHCP server.
As indicated in the answer by Marrten Arias, assign both the client, and the server a static ip address; perhaps something like this:
Server IP: 192.168.01.01
Client IP: 192.168.01.02
Mask (for both): 255.255.255.0
Related
I write a small game using socket in which multiple clients join the game, everything works in the LAN. Now I want to extend to clients outside LAN, what I wonder is: if I don't know the IP address of the client then will the program work?
Inside LAN I use:
char *ip = "127.0.0.1";
The client needs to know the server's IP in order to connect to it.
If the client and server run on the same machine, the server can listen on 127.0.0.1 and the client can connect to that IP.
If the client and server run on the same LAN, the server needs to listen on the NIC that is connected to the LAN, and the client can connect to the server's LAN IP.
If the client is running on a different network and wants to connect to the server over the Internet, the client must connect to the server's public Internet IP, as assigned by the server's ISP. And if the server is running behind a NAT router, the public IP is assigned to the router and not the server directly, so the router must have port forwarding configured to route incoming connections to the server machine on the router's LAN.
If the client doesn't know the IP to connect to, the server will need to publish its IP somewhere that the client can query it, such as via DNS, a website, a central public server that all clients connect to for relaying, etc.
Quick question. It is possible to get adb via ethernet cable connected to my laptop instead of connecting to router?
This is possible, though it's a bit more work. Here are a couple of options:
Run a DHCP service on your laptop to assign the board an IP address.
Connect to the board over serial and assign a static IP address.
If the ethernet port in your laptop doesn't autocross, you may need to connect the devices using a crossover cable instead of a patch cable.
DHCP Service
Ubuntu: Use a local service like avahi-daemon.
Mac OS: Enable internet sharing on the Ethernet port.
Windows: I have never tried, but others have used DHCP Server for Windows for this purpose.
Static IP
Use the serial debug console instructions to connect a serial cable and access the shell.
Connect the Ethernet cable between the board and your computer.
Run the following command as root (replace x.x.x.x with a real IP address):
ip addr add x.x.x.x dev eth0
Verify that the IP address was set properly with ifconfig
Run adb connect x.x.x.x or adb connect Android.local
The static IP address will only stick as long as the Ethernet link is active. If the device reboots or ethernet is unplugged, you'll have to run this procedure again.
How can I get the ip address of the client of which my server is connected to?
Here is how I accept incoming clients:
newsockfd = accept(sockfd, (struct sockaddr*)&clt_addr, &addrlen);
I tried the following:
printf("ip is: %s\n", inet_ntoa(clt_addr.sin_addr));
But I am getting 127.0.0.1 which is not my ip address. Could it have something to do with me using localhost to test my client and server?
Could it have something to do with me using localhost to test my client and server?
Yes. If you connect to a server on localhost the client will also be localhost. This connection is not happening over the Internet, there is no network hardware involved, the client packets will come from localhost. This is known as loopback and occurs entirely in software.
If the client were to connect to the server using the server's external IP address it will have to do a connection via external routing and thus require a routable IP address. For example, I can connect to a server on localhost, but I cannot using my routable IP address because my router blocks incoming connections.
i am working on a remote control program so i need to create connecting to the server(the remote pc) by MAC address (i cant go by ip because he is behind NAT)
It is not possible to connect to a TCP/UDP server by its MAC address, you must use its IP address. Every TCP/UDP server has an IP address, even behind a NAT. TCP/UDP are designed around IP and are transport-agnostic so they can run on networks that are not based on Ethernet/MAC to begin with.
Since your server is behind a NAT, you must connect to the NAT's IP address, and the NAT must be configured to forward that connection to the server's IP address. You cannot avoid that, that is simply how NATs work. If the NAT supports uPNP, the server can programmably configure a port forwarding rule on the NAT. Otherwise the NAT admin must configure it manually.
Why does my DHCP server use my old ip adress as destination?
0.0.0.0 - 255.255.255.255 -> discovery
192.168.0.1 - 192.168.0.100 -> offer
0.0.0.0 - 255.255.255.255 -> request
192.168.0.1 - 192.168.0.100 -> ACK
Normally it should use 255.255.255.255.
Can someone help?
The machine requesting an IP address is not filtering the received packets based on destination address and so it doesn't matter what address is placed as the destination.
The server machine has an ARP entry that connects an IP address to a given Ethernet MAC address (which is how you really contact the client) and so DHCP server process can use that address to create a directed reply rather than broadcasting to the entire subnet. To create a directed reply without some specific IP address would require the DHCP server to create its own UDP packets rather than just letting the system do it.