I am trying to create a chat with Socket. And to do this, I need to use a class in JAVA called Socket. It has, in my case, two parameters: Socket(InetAddress address, int port)
.
I know what is my IP, by the command in CMD: ipconfig
But I don't know what to put in my port number, because I don't know where I can find it.
Is there any command, application or others things to get port number?
I use textpad 8
to create JAVA programs.
There is two programs, the transmitter and the receiver.
Transmitter code
import java.net.*;
import java.io.*;
public class Program
{
public static void main(String[] args)
{
try
{
ServerSocket request = new ServerSocket(12345);
Socket connection = request.accept();
PrintWriter transmitter = new PrintWriter(connection.getOutputStream());
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
do
{
line = keyboard.readLine();
transmitter.println(line);
transmitter.flush();
}
while (line==null || !line.toUpperCase().equals("END"));
transmitter.close();
connection.close();
request.close();
}
catch(Exception error)
{
System.out.println("Communication error");
}
}
}
Receiver code
import java.net.*;
import java.io.*;
public class Programa
{
public static void main(String[] args) {
try
{
Socket connection = new Socket("123.456.78.9",12345);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
BufferedReader receptor = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
String line = null;
do
{
line=receptor.readLine();
System.out.println(line);
}
while(line==null || !line.toUpperCase().equals("END"));
receptor.close();
connection.close();
}
catch(Exception error)
{
System.out.println("Communication error");
System.out.println("Error: " + error);
}
}
}