Client/Server Connection Problem

Basti91

Mitglied
Hi ich habe folgende Klassen programmiert:

Client Klasse:
Java:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {
	Socket client;
	String host;
	
	public Client(String host, int port) throws UnknownHostException, IOException{
			this.host = host;
			client = new Socket(host, port);

	}

	public void sendMessage(String message){
		try {
	       DataOutputStream socketOut = new DataOutputStream(client.getOutputStream());
	       DataInputStream  socketIn  = new DataInputStream(client.getInputStream());
	       //DataInputStream  console   = new DataInputStream(System.in);
	       System.out.println("Connected to " + host + ".\n Sending message");
	
	       boolean done = false;
       
	       while (!done){
	          if (message.equalsIgnoreCase(".bye")){
	             done = true;
	          }
	          else{
	        	  socketOut.writeBytes(message + '\n'); 
	          //!
	          }
	       }
	       /**
	       done = false;
	       while (!done){
	          String line = socketIn.readLine();
	            if (line.equalsIgnoreCase(".bye")){
	               done = true;
	            }
	            else{
	               System.out.println("Server says: " + line);
	            }//!
	          }**/
	       

	       socketOut.close(); socketIn.close(); client.close();
		} 
	    catch (UnknownHostException e){
	    	System.err.println(host + ": unknown host."); } 
	    catch (IOException e) {
	    	System.err.println("I/O error with " + host); }
	}
	

}

Main-Klasse des Clients
Java:
import java.io.*;
import java.net.*;

public class Main{
	
   public static void main(String[] args){
	   Client test;
	try {
		test = new Client("localhost",8082);
		test.sendMessage("Hallo!!");
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
   }
   
  
}


Server:
Java:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
	
	ServerSocket server;
	
	public Server(int port){
		//Creating Server
		try {
		   server = new ServerSocket(port);
		} 
		catch (IOException e) {
		   System.out.println("Error on port: " + port + ", " + e);
		   System.exit(1);
		}
	
	  System.out.println("Server setup and waiting for client connection ...");

	  //Start Listening
      Socket client = null;
      while(true){
      try {
         client = server.accept();
      } 
      catch (IOException e) 
      {
         System.out.println("Did not accept connection: " + e);
         System.exit(1);
      }

      System.out.println("Client connection accepted. Moving to local port ...");

      try{
    	  DataOutputStream streamOut = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
         DataInputStream streamIn = new DataInputStream(new BufferedInputStream(client.getInputStream()));

         boolean done = false;
         String line;
         while (!done){
            line = streamIn.readLine();
            if (line.equalsIgnoreCase(".bye")){
               done = true;
            }
            else{
               System.out.println("Client says: " + line);
            	streamOut.writeBytes("Message erhalten" + '\n');
            }	
         }

         streamIn.close();
         client.close();
      }
      catch(IOException e){
    	  System.out.println("IO Error in streams " + e); 
      }
      }
	}
	
	public void shutdown() throws IOException{
		server.close();
	}
}


Main-Klasse des Servers
Java:
import java.io.IOException;



public class Main {

	public static void main(String[] args) {
		try {
			 final Server test = new Server(8087);
			 
			Runtime.getRuntime().addShutdownHook(new Thread() {
			    public void run() { try {
					test.shutdown();
				} catch (IOException e) {
					e.printStackTrace();
				} }
			}
			);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}





Nun bekomme ich aberfolgende Fehlermeldung wenn ich den Client mit dem Server verbinden will:
Connection refused: connect

Jemand eine Idee wieso der Client sich nicht mit dem Server verbinden kann?
 
Ähm, Server läuft auf 8087, aber Client verbindet sich zu 8082? Das kann nicht gehen...

Nebenbei: Statt "localhost" würde ich (zumindest für den ersten Test) eher auf "127.0.0.1" umstellen. Wäre nicht das erste mal dass irgendwas an der Namensauflösung (ja, auch wenn's nur lokal ist) kaputt ist und "localhost" dann nicht geht.

- Alex
 

Zurück
Oben