Reputation: 45
I am using threads and sockets for the first, so I'm trying to make a chat (local)
ClientA (Actually a server) :
public class ClientA {
public static void main(String[] args) {
ServerSocket servsock = null;
try {
servsock = new ServerSocket(9998);
Socket conn = servsock.accept();
System.out.println("server socket listening on local port : " + servsock.getLocalPort());
System.out.println("socket listening on local port : " + conn.getLocalPort());
System.out.println("socket listening on port : " + conn.getPort());
servsock.close();
TReadingSock tls = new TReadingSock(conn);
tls.start();
TWritingSock tes = new TWritingSock(conn);
tes.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client B, the actual client :
public class ClientB {
public static void main(String[] args) {
Socket sock = null;
try {
sock = new Socket("localhost", 9998);
System.out.println("socket listening on local port : " + sock.getLocalPort());
System.out.println("socket listening on port : " + sock.getPort());
} catch (IOException e) {
e.printStackTrace();
}
TReadingSock tls = new TReadingSock(sock);
tls.start();
TWritingSock tes = new TWritingSock(sock);
tes.start();
}
}
The print show this :
Client A:
server socket listening on local port : 9998
socket listening on local port : 9998
socket listening on port : 50875
Client B:
socket listening on local port : 50875
socket listening on port : 9998
So, as stupid as it appears, I have serious problems understanding the whole socket thing, the only thing I get here is that my programs are listening on different ports, and I don't see why. I guess it must appear pretty obvious to some, so I'm requesting your help to enlight me !
Thanks in advance.
EDIT : Thanks you for all your answers. The thing is, for me you need a ServerSocket to listen on a port and accept any client trying to connect. When it's done, it returns a Socket object, which I pass to my threads so they can do their jobs. That's for ClientA. Client B just creates a Socket, which will try to connect to the 9998 port, which is the one I listen on with my ServerSocket.It then passes it to its thread. The thing is, when I launch my Clients (A then B), and I write in console A, nothing happens on console B (maybe I should have specified that earlier, sorry).
I guessed it was due to the ports my sockets are using. I checked a few tutorials and, even after reading your responses, I don't see what's wrong, so either I'm missing some very obvious notions, or it doesn't come the connection itself but the way I handle my connection. Just in case, I will post the contents of threads :
public class TWritingSock extends Thread {
private Socket sockw;
public TWritingSock(Socket sockw){
this.sockw = sockw;
}
@Override
public void run(){
try {
while(true){
//Recuperation des saisies clavier
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Ecriture de la saisie
PrintWriter pw= new PrintWriter(sockw.getOutputStream(), true);
String typed = br.readLine();
if(typed.equals("fin")){
//sockw.close();
System.out.println("fermé");
break;
}
pw.write(typed);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
sockw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And
public class TReadingSock extends Thread {
private Socket sockr;
public TReadingSock(Socket sockr){
this.sockr = sockr;
}
@Override
public void run(){
try {
while(true){
BufferedReader br = new BufferedReader(new InputStreamReader(sockr.getInputStream()));
String typed = br.readLine();
if(typed.equals("fin")){
//sockr.close();
System.out.println("fermé");
break;
}
System.out.println(typed);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
sockr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 1
Views: 1746
Reputation: 84151
Each TCP connection on the Internet is made up of two pairs, or four elements: end-point A IP address, end-point A port number, and end-point B IP address, end-point B port. At the transport level the addresses are designated in the IP packet header, while ports are noted in the TCP header. That allows for unambiguous identification of a host and a process (by way of the port) to deliver the packet to.
When you bind()
your listening "server" socket you fix that end-point port number (9998 in your case). When client connection arrives the other three parts are also fixed - your local IP is of the network interface where the connection arrived at, and the remote IP and the port are of the connecting client, where the port is usually picked by the OS from the pool of ephemeral ports, 50875 in your example.
Upvotes: 1
Reputation: 2260
(I'll be using crude and simple images to talk about a Socket, please see it as my attempt to explain this as simply as possible.)
See the Socket as a tunnel. Now, if I tell you that my end of the tunnel is 9998, you can then reach it, but your end of this tunnel can be anything, it's up to you to decide.
ClientA (which is in fact Server), is saying "I want to listen on port 9998." and when ClientA connects "Ok we are connected, I am on port 9998 and you on 50875.".
ClientB is saying "I want to reach your port 9998, and I'll be using my port 50875 to do so."
Both applications can now communicate via the tunnel because they know where it goes.
Upvotes: 2
Reputation: 9574
Ok, let me try to make it simple for you to understand the concepts. So that when you read the tutorial it will all make sense.
Sockets are in very simple terms, a tunnel between your client and server. When one reads, the other writes. In other terms when one waits to read, the other writes into the tunnel. When the read is successful, the data can be processed by the receiver.
Here both client and server can do a 2 way communication. Some people create a zigzag kinda pattern, server waits for read, client sends data and goes into read wait, server processes it and responds back to client. Client displays success/error. So on so forth.
Before they can do the read and write, they form a connection. To form the connection they need to tell each other what their identity (IP address/DNS) is/are.
Got it this far ?
Let me know if you need me to explain ports.
Upvotes: 0