Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: Java  |  size: 1.89 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.IOException;
  2. import java.io.ObjectInputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.HashMap;
  7. import java.util.LinkedList;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class Server {
  12.  
  13.         int port = 8888;
  14.         ServerSocket socket;
  15.         HashMap<String, Calendar> hashmap = new HashMap<String, Calendar>();
  16.         ObjectOutputStream out;
  17.         ObjectInputStream in;
  18.        
  19.         public Server () {
  20.                 try {
  21.                         socket = new ServerSocket(port);
  22. //                      Calendar calendar = new Calendar();
  23.                        
  24.                         System.out.println("Server gestartet...");
  25.                        
  26.                        
  27.                 } catch (IOException e) {
  28.                         e.printStackTrace();
  29.                 }
  30.                
  31.                 while(true){
  32.                         listen();
  33.                 }
  34.         }
  35.        
  36.        
  37.         public void connect(){
  38.                 try {
  39.                         Socket client = socket.accept();
  40.                         out = new ObjectOutputStream(client.getOutputStream());
  41.                         in = new ObjectInputStream(client.getInputStream());
  42.                 } catch (IOException e) {
  43.                         // TODO Auto-generated catch block
  44.                         e.printStackTrace();
  45.                 }
  46.         }
  47.        
  48.         public void listen(){
  49.                 try {
  50.                        
  51.                        
  52.                        
  53.                         Object o = in.readObject();
  54.                        
  55.                        
  56.                         if(o instanceof String){
  57.  
  58.                                 out.writeObject(hashmap.get((String)o));
  59.  
  60.                         }
  61.                        
  62.                         else {
  63.                                 if (o instanceof LinkedList){
  64.                                         System.out.println("Der Client sendet eine Liste...");
  65.                                         LinkedList<Date> list = new LinkedList<Date>();
  66.                                         list = (LinkedList<Date>)o;
  67.                                         out.writeObject(1);
  68.                                        
  69.                                 }
  70.                                
  71.                         }
  72.                        
  73.                        
  74.                        
  75.                 } catch (IOException e) {
  76.                         // TODO Auto-generated catch block
  77.                         e.printStackTrace();
  78.                 } catch (ClassNotFoundException e) {
  79.                         // TODO Auto-generated catch block
  80.                         e.printStackTrace();
  81.                 }
  82.         }
  83.        
  84.         public boolean nameVorhanden(String calname){
  85.                
  86.                 if(hashmap.get(calname)==null){
  87.                         return false;
  88.                 }
  89.                 else{
  90.                         return true;
  91.                 }
  92.                
  93.         }
  94.        
  95.         public static void main(String[] args) {
  96.                
  97.                 Server server = new Server();
  98.                 server.connect();
  99.                
  100.         }
  101.        
  102.  
  103.        
  104.  
  105. }