Read data from a particular port and specified IP using java - java

I would like to read the messages from a particular port.For example the IP is 1.2.3.4 and the port is 1000. Already the IP is used for receiving some messages. What I would like to do is to listen to that particular IP and receive all the messages using a java program. Will
SocketServer do the purpose??
ServerSocket ss = new ServerSocket(1000);
Socket socket = new Socket("1.2.3.4",1000);
socket = ss.accept();
Is it possible to read every contents that are being received by the particular IP and port?

To listen to a specific address you have to create a ServerSocket like this
ServerSocket ss = new ServerSocket(); // Unbound socket
ss.bind(new InetSocketAddress("1.2.3.4", 1000)); // Bind the socket to a specific interface
Socket client = ss.accept();
This way the server socket is bound to a specific network interface and will only receive incoming connections from it.

Related

TCP socket connection over internet

I am doing a project for which connection between server and client is required.
I did it by adding TCP sockets.
Here is the code fraction :
Server:
ServerSocket welcomeSocket = new ServerSocket(80);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
WorkerThread wt = new WorkerThread(connectionSocket, id);
Thread t = new Thread(wt);
t.start();
workerThreadCount++;
}
Client :
Socket skt = new Socket("192.168.0.108", 80); // The IP address is from cmd->ipconfig/all-> IPv4 Address
outToServer = new PrintWriter(skt.getOutputStream(), true);
inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
It all works when both ends are in same device/under same WiFi.But I don't understand what to do for creating connection over internet.
Please help with clear steps.
Here:
Socket skt = new Socket("192.168.0.108", 80);
That is local address. If you want to have a server that is reachable on the internet, then that server needs to have its global public IP address!
In other words: you have to make sure that the server can be reached from the internet somehow. For example by turning to some service provider that hosts servers that you can then equip with your code!
The whole purpose of 192.168 addresses is to be defined only in a local subnet.
Alternatively, you have to check if your ISP has a service where the ISP assigns an IP address to your connection, and that allows calls from the internet to go to your "place".
Meaning: when you want to receive phone calls, you need a phone that is connected to the phone net!
In order to connect to a socket over WAN, you must port forward that port to your local device. This can be done in your routers' settings.
192.168.0.108 --> That's your local IP-address.
This can be used on your local network without any requirements for port forwarding whatsoever. However, to use it over WAN, execute the following steps:
Step 1: Search for your routers' model number and port forwarding on Google on how-to forward port 80 to your local IP-address. Warning: use a static IP-address on your local device to prevent your IP from changing after a reboot.
Step 2: Go to a website like IP Chicken and find your external IP-address.
You can then connect to your socket using:
Socket skt = new Socket("[EXTERNALIP]", 80);
Please be noticed: unless you have a business network, your external IP-address will probably change from time to time.

Is it possible to transfer (a String, for example) from a TCP Server to a UDP Client?

I'm working on TCP and UDP just to learn the basics of how networks and protocols work in Java, and the task my professor has set me is to have the following:
A TCP Client that connects to a TCP Server, which communicates with a UDP Client that connects to a UDP Server, which gets/alters data from a Database.
I have everything set up and working, but right now the TCP Server calls the UDP Client as an object, and passes its information through a method call, rather than over the internet.
For TCP I'm using server sockets and input stream / buffered stream readers, and for the UDP I'm using datagram packets. So I'm not really sure how I can send from one to the other directly, or if it's even possible.
Thanks in advance for any help/advice/guidance!
Jona
**EDIT:
What I mean by passing through a method call:
//In TCPServer class
UDPClient udpc = new UDPClient();
String result = udpc.GetFromUDPServer(clientCommand);
//In UDPClient class
public class UDPClient{
public String GetFromUDPServer(String clientCommand){
try{
//Create a datagram socket
DatagramSocket sock = new DatagramSocket();
byte[] buffer = clientCommand.getBytes("UTF-8");
InetAddress ipAddress = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ipAddress, 447);
...

Client-Server application on different computers

I want to modify my code to make the clients connect to the server if the server is hosted on a different machine. This is the code I have now:
Server:
int port = 5000;
ServerSocket server = new ServerSocket(port);
Socket socket = server.accept();
output/input streams....
Client:
String host = "localhost";
int port = 5000;
Socket socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
I am aware of the inetAddress and that's what I need help with, I don't know how to use it correctly, I don't want any complicated methods just enough to connect them together. Also I want to know if it is possible to use DNS for this or if I need a web server or something similar (of course DNS is purely curiosity).
To connect to a remote machine you need that machines IP address or hostname, then you can use InetAddress.getByName(String).
You'd use the IP address if there's no DNS server available to resolve the IP from the hostname, like this:
Socket socket = new Socket(InetAddress.getByName("1.2.3.4"), 5000);
Or is there's is a DNS server, then you can just pass the servers hostname like this:
Socket socket = new Socket(InetAddress.getByName("myserver"), 5000);

Bind an interface in java TCP connection

I have two interfaces in a solaris host. I would like to initiate two TCP connections to a single TCP server via both interfaces as shown in the diagram. Are there any options in Java to bind the interface to the TCP socket to override the local routing table?
I am attaching the network diagram,
I would like to use both the serial links bandwidth to get the data from server. Hence I would like to initiate the connection on both the interfaces.
thanks,
You can use
Socket s = new Socket(hostname, port, localInterface, 0);
However, many OSes do not honour this "hint" and will use the routing table anyway.
Do you mean something like this:
Socket socket1 = new Socket();
socket1.bind(new InetSocketAddress("10.1.1.1", port));
socket1.connect(new InetSocketAddress("10.1.3.1", port));
Socket socket2 = new Socket();
socket2.bind(new InetSocketAddress("10.1.2.1", port));
socket2.connect(new InetSocketAddress("10.1.3.1", port);

Communiation with multiple clients via Sockets

I have a Server Socket and 3-4 android devices as clients. I'm using TCP/IP for communications. Which is the best method. Should I use multiple ports for each client? Or should I use same port. If using same function then how should I identify the communication addressed to different devices?
No, you do not need several ports.
ServerSocket server = new ServerSocket(port);
while (true)
{
Socket socket = server.accept();
// do something with this socket - aka 1 client
new SomeClientClass(socket);
InputStream in = socket.getInputStream();
in.read(byte[]);
OutputStream out = socket.getOutputStream;
// out will only write response to its own client.
// when this new SomeClientClassis created, method returns to this point
// in while loop and waits for the next client
}
You can use one port. The client can send you its id. If it can't you can look at the clients IP address to workout which one it is.
There are thousands of TCP client/server code examples on the web, but I would start with the sample code which comes with the JDK,

Categories

Resources