Netzwerkrprogammierung Client - Server

Status
Nicht offen für weitere Antworten.

John_J

Mitglied
Hallo,

also ich hab ein Verständisprobleme bei einer Aufgabe. Der Client soll dem Server, inneralb einer for schleife die zahlen 0-10 weitergeben, der server soll dann jeweils 0*0 , 1*1 bis 10*10 berechnen und es dem Client zurückgeben, leider bekomme ich das nicht so hin wie ich es haben will, kann mir da einer vielleicht helfen.

hier meine Codes:

Code:
import java.io.*;
import java.net.*;

public class MulServer2
{
  private static void handleConnection( Socket client ) throws IOException
  {
    InputStream  in  = client.getInputStream();
    OutputStream out = client.getOutputStream();

   int c;
   
   while( (c = in.read()) != -1 )
   {



     int factor1 = in.read();
     int factor2 = in.read();

     out.write( factor1 * factor2 );







    }






  }

  public static void main( String[] args ) throws IOException
  {
    ServerSocket server = new ServerSocket( 3141 );

    while ( true )
    {
      Socket client = null;

      try
      {
        client = server.accept();
        handleConnection ( client );
      }
      catch ( IOException e ) {
        e.printStackTrace();
      }
      finally {
        if ( client != null )
          try { client.close(); } catch ( IOException e ) { }
      }
    }
  }
}

Code:
import java.net.*;
import java.io.*;

class MulClient2
{
  public static void main( String[] args )
  {
    Socket server = null;

    try
    {
      server = new Socket( "localhost", 3141 );
      InputStream  in  = server.getInputStream();
      OutputStream out = server.getOutputStream();


       for(int i=0; i<10; i++)
       {
        out.write(i);
        out.write(i);
       }









      int k;


      /*out.write( 4 );
      out.write( 9 );        */

       while( (k = in.read()) != -1 )
    {

       int result = in.read();
      System.out.println( result );








      }


    }
    catch ( UnknownHostException e ) {
      e.printStackTrace();
    }
    catch ( IOException e ) {
      e.printStackTrace();
    }
    finally
    {
      if ( server != null )


        try { server.close(); } catch ( IOException e ) { }
    }
  }
}
 
Code:
public class Test {

	public static void main(String[] args) {

		new Thread() {
			public void run() {
				try {
					MulServer2.main(null);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}.start();
		new Thread() {
			public void run() {
				try {
					Thread.sleep(400);
					MulClient2.main(null);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}.start();
	}
}

class MulServer2 {
	private static void handleConnection(Socket client) throws IOException {
		InputStream in = client.getInputStream();
		OutputStream out = client.getOutputStream();

		int c;

		while ((c = in.read()) != -1) {

			int factor1 = c;
			int factor2 = in.read();
			System.out.println("read: " + factor1 + ", " + factor2);
			out.write(factor1 * factor2);
			out.flush();
		}

	}

	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(3141);

		while (true) {
			Socket client = null;

			try {
				client = server.accept();
				handleConnection(client);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (client != null)
					try {
						client.close();
					} catch (IOException e) {
					}
			}
		}
	}
}

class MulClient2 {
	public static void main(String[] args) {
		Socket server = null;

		try {
			server = new Socket("localhost", 3141);
			InputStream in = server.getInputStream();
			OutputStream out = server.getOutputStream();

			for (int i = 0; i < 10; i++) {
				System.out.println("write " + i);
				out.write(i);
				out.write(i);
				out.flush();
			}

			int k;

			/*
			 * out.write( 4 ); out.write( 9 );
			 */

			while ((k = in.read()) != -1) {

				int result = k;
				System.out.println(result);

			}

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (server != null)

				try {
					server.close();
				} catch (IOException e) {
				}
		}
	}
}
funktioniert in diesem Fall aber sollte noch dringend geändert werden:
in.read() liest nur ein Byte, einen Wert 0-255,
wenn du einen großen Integer 45379079 sendest, so sind das bis zu 4 Bytes, die einzeln eingelesen werden,
verwende lieber DataInputStream und readInt()
 
hallo,

ich hab so gemacht, wie du es gemeint hast.... jetzt spackt er aber voll ab, :-( kommen komische zahlen raus....

was müsste ich bei Double machen, kannste mir dabei auch helfen ... sry für die dummen Fragen :-( ... hab in 3 wochen ne wichtige Prüfung .... und Client/Server + Threads muss ich gut beherrschen.




Code:
  import java.net.*;
import java.io.*;

class MulServer4 {
   private static void handleConnection(Socket client) throws IOException {
      InputStream in = client.getInputStream();
      OutputStream out = client.getOutputStream();
       DataInputStream in1= new DataInputStream(in);

      int c;

      while ((c = in1.readInt()) != -1) {

         int factor1 = c;
         int factor2 =in1.readInt();
         int factor3 =in1.readInt();
         System.out.println("read: " + factor1 + ", " + factor2);
         out.write(factor1 * factor2*factor3);
         out.flush();
      }

   }

   public static void main(String[] args) throws IOException {
      ServerSocket server = new ServerSocket(3141);

      while (true) {
         Socket client = null;

         try {
            client = server.accept();
            handleConnection(client);
         } catch (IOException e) {
            e.printStackTrace();
         } finally {
            if (client != null)
               try {
                  client.close();
               } catch (IOException e) {
               }
         }
      }
   }
}

Code:
 import java.net.*;
import java.io.*;

class MulClient4 {
   public static void main(String[] args) {
      Socket server = null;

      try {
         server = new Socket("localhost", 3141);
         InputStream in = server.getInputStream();
         DataInputStream in1= new DataInputStream(in);
         OutputStream out = server.getOutputStream();

         for (int i = 0; i < 10; i++) {
            System.out.println("write " + i);
            out.write(i);
            out.write(i);
            out.write(i);
            out.flush();
         }

         int k;

         /*
          * out.write( 4 ); out.write( 9 );
          */

         while ((k = in1.readInt()) != -1) {

            int result = k;
            System.out.println(result);

         }

      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (server != null)

            try {
               server.close();
            } catch (IOException e) {
            }
      }
   }
   
 }
 
ok, write(int) schreibt auch nur ein Byte, wie in der API zu der Methode nachzulesen,
du brauchst analog auch einen DataOutputStream und die Methode writeInt statt write
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben