Ich habe ein Client - Server Paar geschrieben aber irgendwie funktionieren die wie sie sollte. Der Client kann senden aber nicht empfangen. Der Code :
Java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ue.cln;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args)throws IOException, UnknownHostException{
System.out.println("*CLIENT*");
Socket svSocket = null;
PrintWriter out = null;
BufferedReader inps = null;
try{
svSocket = new Socket("localhost", 49000);
out = new PrintWriter(svSocket.getOutputStream(), true);
inps = new BufferedReader(new InputStreamReader(svSocket.getInputStream()));
}catch(UnknownHostException e){
}catch(IOException e){
}
// Speichere den standard Input
InputStreamReader sIN = new InputStreamReader(System.in);
BufferedReader standardIN = new BufferedReader(sIN);
String toServer = null;
String fromServer = null;
out.println("Hallo von" + svSocket.getLocalSocketAddress());
while((fromServer = inps.readLine())!= null ){
System.out.println("ICH " + toServer);
fromServer = inps.readLine();
System.out.println("Server : " + fromServer);
}
out.close();
inps.close();
standardIN.close();
svSocket.close();
}
}
Alles anzeigen
Java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ue.srv;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException{
System.out.println("*SERVER*");
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(49000);
}catch(IOException e){
System.err.println("Socket number do");
System.exit(1);
}
//System.out.println();
//System.out.println("Server name " + serverSocket.toString());
Socket clientSocket = null;
try{
clientSocket = serverSocket.accept();
System.out.println("Verbunden mit " + clientSocket.getRemoteSocketAddress());
}catch(IOException e){
System.err.println("accept() failed big time");
System.exit(1);
}
// Socket Output
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
//Socket Input
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String toClient, fromClient;
//toClient = " Hallo Client";
//out.println(toClient);
//Input von der Console
BufferedReader standardIn = new BufferedReader(new InputStreamReader(System.in));
while((fromClient = in.readLine()) != null){
System.out.println("Client : " + fromClient );
toClient = standardIn.readLine();
System.out.println(" ICH : " + toClient);
out.println(toClient);
if(fromClient.equals("!exit")) break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
Alles anzeigen
Sollte ich vielleich beim Server Threads verwenden?