SHARE
TWEET

Untitled

a guest Aug 12th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Java Server:
  2.  
  3.  // Server.java
  4.  
  5.  // import java.net.ServerSocket;
  6.  // import java.net.Socket;
  7.  import java.io.*;
  8.  
  9.  public class Server {
  10.     public static void main(String[] args) {
  11.         Server server = new Server();
  12.         try {
  13.             server.test();
  14.         } catch (IOException e) {
  15.             e.printStackTrace();
  16.         }
  17.     }
  18.     void test() throws IOException {
  19.         System.out.println("Server am starten");
  20.         int port = 5555;
  21.         java.net.ServerSocket serverSocket = new java.net.ServerSocket(port);
  22.         java.net.Socket client = warteAufAnmeldung(serverSocket);
  23.         String nachricht = leseNachricht(client);
  24.         System.out.println(nachricht);
  25.         schreibeNachricht(client, nachricht);
  26.     }
  27.     java.net.Socket warteAufAnmeldung(java.net.ServerSocket serverSocket) throws IOException {
  28.         System.out.println("warte auf Client-Anmeldung");
  29.         java.net.Socket socket = serverSocket.accept(); // blockiert, bis sich ein Client angemeldet hat
  30.         System.out.println("Client hat sich angemeldet");
  31.         return socket;
  32.     }
  33.     String leseNachricht(java.net.Socket socket) throws IOException {
  34.         System.out.println("Lese Nachricht");
  35.         BufferedReader bufferedReader =
  36.             new BufferedReader(
  37.                 new InputStreamReader(
  38.                     socket.getInputStream()));
  39.         char[] buffer = new char[200];
  40.         int anzahlZeichen = bufferedReader.read(buffer, 0, 200); // blockiert bis Nachricht empfangen
  41.         String nachricht = new String(buffer, 0, anzahlZeichen);
  42.         return nachricht;
  43.     }
  44.     void schreibeNachricht(java.net.Socket socket, String nachricht) throws IOException {
  45.         PrintWriter printWriter =
  46.             new PrintWriter(
  47.                 new OutputStreamWriter(
  48.                     socket.getOutputStream()));
  49.         printWriter.print(nachricht);
  50.         printWriter.flush();
  51.     }
  52.  }
  53.  
  54.  
  55. TCPDump am Server:
  56.  
  57.  
  58. root@goofy:/home/daniel# tcpdump port 12345
  59. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  60. listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
  61. 00:44:14.102914 IP 201.159.7.69.30767 > goofy.ask.fh-furtwangen.de.12345: S 835715488:835715488(0) win 8192 <mss 1380,nop,wscale 2,nop,nop,sackOK>
  62. 00:44:17.101054 IP 201.159.7.69.30767 > goofy.ask.fh-furtwangen.de.12345: S 835715488:835715488(0) win 8192 <mss 1380,nop,wscale 2,nop,nop,sackOK>
  63. 00:44:23.099970 IP 201.159.7.69.30767 > goofy.ask.fh-furtwangen.de.12345: S 835715488:835715488(0) win 8192 <mss 1380,nop,nop,sackOK>
RAW Paste Data
Top