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();
}
}
}
}