TCP MultiThreaded Server und Client - irgendwo ist der Fehler, aber ich find ihn nicht

JasminM

Mitglied
Hallo,

Ein relativ einfaches Beispiel:
ich will einen Client aufsetzen, der eine Zahl ausgibt, die an einen TCP Server gesendet wird. Dieser berechnet das Quadrat daraus und schickt diese Zahl zurück. Leider gibt mir meine Konsole nur zwei mal die gleiche Zahl raus. Es wäre nett, wenn jemand mal drüber schauen kann.

Client:

Java:
package Klausuren;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;

public class TCPClient2013 {
	
	static int toInt(byte[] bytes) {
		
		return ByteBuffer.wrap(bytes).getInt();
	}

	static byte[] toBytes(int value){
		
		return ByteBuffer.allocate(4).putInt(value).array();
	}
	
	static int zahl = (int) (Math.random() * 32767);
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		@SuppressWarnings("resource")
		Socket s = new Socket("localhost", 6634);
		OutputStream out = s.getOutputStream();
		InputStream in = s.getInputStream();
		
		out.write(zahl);
		
		System.out.println(zahl);
		
		in.read();
		
		System.out.println(zahl);
		
		
		
		

	}

}


Server

Java:
package Klausuren;

import java.io.IOException;
import java.net.ServerSocket;


public class TCPServer2013 {
	


	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	
		
		ServerSocket s = null;
		
		
		
		try{
			s = new ServerSocket(6634);
			
			while(true){
				
				 new TCPServerThread2013(s.accept()).start();
				
			}


			
			
			
			
		}catch(IOException ioe){ioe.printStackTrace();};

	}

}


Thread

Java:
package Klausuren;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;

public class TCPServerThread2013 extends Thread{
	
	static int toInt(byte[] bytes){
		
		return ByteBuffer.wrap(bytes).getInt();
	
	}
	
	static byte[] toBytes(int value){
		
		return ByteBuffer.allocate(4).putInt(value).array();
	}
	
	
	Socket s;
	
	public TCPServerThread2013(Socket socket){
		this.s = socket;
	}
	
	
	
	public void run(){
		
		try{
			
		InputStream in = s.getInputStream();
		
		OutputStream out = s.getOutputStream();
		
		int temp = in.read();
		
		int zahl = temp*temp;
		
		out.write(zahl);
		
		out.flush();
		
		in.close();
		out.close();
		
		
		
			
		}catch(Exception e){};
		
	}

}
 
Ich hab zwar noch nie son Server-Client Programm aufgebaut, vllt. hilft dir diese Anmerkung trotzdem:

Java:
..........
static int zahl = (int) (Math.random() * 32767);
 
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
 
		@SuppressWarnings("resource")
		Socket s = new Socket("localhost", 6634);
		OutputStream out = s.getOutputStream();
		InputStream in = s.getInputStream();
 
		out.write(zahl);
 
		System.out.println(zahl);
 
		in.read();
 
		System.out.println(zahl);
..........

Du gibst hier einfach nochmal die Zahl aus, welche oben deklariert und initialisiert wurde, oder? Wieso sollte die verändert sein?
 

Zurück
Oben