Salve,
allora
SERVER
codice:
class server2 {
public static void main(String args[]) throws IOException {
String data = "Welcome to My Server"; //welcome message
String data1 = "enter username"; //welcome message
String message,servermessage;
int idstats1 = 0; //verifyed username
int idstats2 = 0; //verifyed password
int verify1 = 0;
try {
//Detecting the localhost's ip address
InetAddress localaddr = InetAddress.getLocalHost();
System.out.println ("Local IP Address : " + localaddr );
System.out.println ("Local hostname : " + localaddr.getHostAddress());
//Creating a server socket for connection
ServerSocket srvr = new ServerSocket(1234);
System.out.println("Waiting for connection on "+localaddr);
while(true){
//Accept incoming connection
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
//get Input and Output streams
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
System.out.print("Sending string: '" + data + "'\n");
out.println(data); //sends welcome message
System.out.print("Sending string: '" + data1 + "'\n");
out.println(data1); //sends welcome message
}
} catch(BindException e) {
e.printStackTrace();
System.out.print("A server is already running on the same port.");
} catch(SocketException e) {
e.printStackTrace();
System.out.print("Client has disconnected rudely.");
} catch(Exception e) {
e.printStackTrace();
System.out.print(e);
}
}}
CLIENT
codice:
class LongClient {
public static void main(String args[]) {
String data="ciao";
String data1="ciao2";
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
BufferedWriter in2 = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
Scanner kbReader = new Scanner(System.in);
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.flush();
String message, servermessage;
InetAddress clientip = InetAddress.getLocalHost();
System.out.println("Preparing to chat...");
out.println("Client IP Address: " + clientip); // sends message to client giving ip
out.println("Client hostname: " + clientip.getHostAddress()); // send message to client giving computer name
if (in.ready()) {
servermessage = in.readLine();
System.out.println("server>: " + servermessage);
}
if (in.ready()) {
servermessage = in.readLine();
System.out.println("server>: " + servermessage);
}
ServerSocket srvr = new ServerSocket(1234);
//Accept incoming connection
Socket skt2 = srvr.accept();
System.out.print("Server has connected!\n");
//get Input and Output streams
out.flush();
System.out.print("Sending string: '" + data + "'\n");
out.println(data); //sends welcome message
message = kbReader.nextLine();
out.println(message);
out.println("Done");
//message="bye";
//out.println("bye");
Thread.currentThread().sleep(300);
//get Input and Output streams
System.out.print("Sending string: '" + data + "'\n");
out.println(data); //sends welcome message
System.out.print("Sending string: '" + data1 + "'\n");
out.println(data1); //sends welcome message
in2.write(data, 200, 200);
//reads the line typed in
out.close();
in.close();
} catch(Exception e) {
System.out.print(e);
}
}
}
Ora il mio problema e che come fa il client ad inviare messaggi al server.
Il server invia correttamente i due messaggi il client ma non riesco a inviare i messagi dal client al server. Come posso risolvere?