Hej.
Pisze prosty program na zajęcie ale mi nie działa... Program ma:
- Łączyć się z serwerem.
- wysyłać numer nowego portu - >serwer ma tworzyć połączenie na nowym porcie
- Połączyć się na nowym porcie
- Klient ma zamknąć połączenie
Niesyty wygląda że serwer nie otwiera nowego połączenia. Zatrzymuje się w okolicach linijki clientSocket = serverSocket.accept(); <-bez błędu
Czy ktoś wie co może być nie tak?
Output:
Podaj PORT: 9997
Server - Connected
Client - Connected
Klient - Podaj nowy port: 9998
Server input: port
Server new port: 9998
Server - Connected closed
Client - Close connection
Client - connected error
Could not connect to localhost:9997
Java Result: 1
package tsclisrv;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.Thread.sleep;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
private static int PORT = 0;
static boolean flaga = true;
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static PrintWriter out;
private static BufferedReader in;
static public void setDefaultPort(int port) {
PORT = port;
}
private static boolean connect() throws IOException, InterruptedException {
close();
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
System.err.println("Could not listen on port: " + PORT);
System.exit(1);
}
clientSocket = null;
try {
clientSocket = serverSocket.accept();
flaga = false;
} catch (IOException e) {
System.out.println("Server - connected error");
System.err.println("Accept failed.");
//t.interrupt();
System.exit(1);
}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("Server-OK");
System.out.println("Server - Connected");
return true;
}
private static void close() throws IOException {
if (serverSocket == null) {
return;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
serverSocket = null;
System.out.println("Server - Connected closed");
}
public static void main(String[] args) throws IOException, InterruptedException {
/*Read data*/
Scanner systemIn = new Scanner(System.in);
if (PORT == 0) {
System.out.print("Podaj PORT dla serwera: ");
PORT = systemIn.nextInt();
}
connect();
/*GO*/
try {
while (true) {
Scanner scIn = new Scanner(in);
String input = scIn.next();
System.out.println("Server input: " + input);
if (input.equals("close")) {
out.print("close");
break;
} else if (input.equals("port")) {
PORT = scIn.nextInt();
System.out.println("Server new port: " + PORT);
connect();
} else {
System.out.println("Server: no-command " + input);
out.println("no-command");
break;
}
}
} catch (Exception e) {
System.out.println("Server Exception");
Logger.getLogger(TScliSrv.class.getName()).log(Level.SEVERE, null, e);
} finally {
try {
close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Close server");
}
}
package tsclisrv;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.Thread.sleep;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
private static int PORT = 0;
private static String HOST;
private static Socket socket = null;
private static PrintWriter out;
private static BufferedReader in;
static public void setDefaultPort(int port) {
PORT = port;
}
static public void setDefaultHost(String host) {
HOST = host;
}
public static void connect() throws IOException {
close();
try {
socket = new Socket(HOST, PORT);
} catch (Exception e) {
System.out.println("Client - connected error");
System.err.println("Could not connect to " + HOST + ":" + PORT);
System.exit(1);
}
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client - Connected");
}
private static void close() throws IOException {
if (socket == null) {
return;
}
out.close();
in.close();
socket.close();
socket = null;
System.out.println("Client - Close connection");
}
public static void main(String[] args) throws IOException, InterruptedException {
/*Read data*/
Scanner systemIn = new Scanner(System.in);
if (PORT == 0) {
System.out.print("Klient - Podaj PORT dla klienta: ");
PORT = systemIn.nextInt();
}
if (HOST == null) {
System.out.print("Klient - Podaj HOST dla klienta: ");
HOST = systemIn.next();
}
connect();
/*GO*/
sleep(1000);
System.out.print("Klient - Podaj nowy port: ");
int PORT = systemIn.nextInt();
out.println("port " + PORT);
//System.out.println("Klient - Czekam... Polaczyc?");
//systemIn.next();
sleep(7000);
connect();
out.println("close");
close();
System.out.println("Client - Close client");
}
}
package tsclisrv;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TScliSrv {
private static int port = 0;
private static String host;
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
/*READ*/
Scanner systemIn = new Scanner(System.in);
System.out.print("Podaj PORT: ");
port = systemIn.nextInt();
host = "localhost";
/*GO*/
SrverThread srv = new SrverThread();
srv.start();
ClientThread client = new ClientThread();
client.start();
}
static class SrverThread extends Thread {
@Override
public void run() {
try {
Server.setDefaultPort(port);
Server.main(new String[]{});
} catch (IOException ex) {
Logger.getLogger(TScliSrv.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(TScliSrv.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
static class ClientThread extends Thread {
@Override
public void run() {
try {
Client.setDefaultPort(port);
Client.setDefaultHost(host);
Client.main(new String[]{});
} catch (IOException ex) {
Logger.getLogger(TScliSrv.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(TScliSrv.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}