Advertisement
Guest User

Untitled

a guest
Jan 31st, 2013
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. package test1;
  2.  
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.net.InetAddress;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. /**
  12.  *
  13.  * @author Vallentin <vallentinsource@gmail.com>
  14.  * @since April 8, 2012
  15.  *
  16.  */
  17.  
  18. public class Client
  19. {
  20.     public static Socket socket;
  21.    
  22.     public static int port = 2406;
  23.     public static String ip = "";
  24.    
  25.    
  26.    
  27.     public static void main(String[] args)
  28.     {
  29.         try
  30.         {
  31.             String local;
  32.            
  33.             try
  34.             {
  35.                 local = InetAddress.getLocalHost().getHostAddress() + ":" + port;
  36.             }
  37.             catch (UnknownHostException ex)
  38.             {
  39.                 local = "Network Error";
  40.             }
  41.            
  42.             ip = (String) JOptionPane.showInputDialog(null, "IP: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, local);
  43.            
  44.             port = Integer.parseInt(ip.substring(ip.indexOf(":") + 1));
  45.             ip = ip.substring(0, ip.indexOf(":"));
  46.            
  47.             socket = new Socket(ip, port);
  48.            
  49.             String username = System.getProperty("user.name");
  50.             username = (String) JOptionPane.showInputDialog(null, "Username: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, username);
  51.            
  52.             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  53.             oos.writeObject(username);
  54.            
  55.             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
  56.             String response = (String) ois.readObject();
  57.            
  58.             JOptionPane.showMessageDialog(null, response, "Message", JOptionPane.INFORMATION_MESSAGE);
  59.         }
  60.         catch (Exception ex)
  61.         {
  62.             JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Alert", JOptionPane.ERROR_MESSAGE);
  63.             System.exit(0);
  64.         }
  65.        
  66.     }
  67. }
  68.  
  69. package test1;
  70.  
  71. import java.io.Serializable;
  72.  
  73. /**
  74.  *
  75.  * @author Vallentin <vallentinsource@gmail.com>
  76.  * @since April 8, 2012
  77.  *
  78.  */
  79.  
  80. public class DataPackage implements Serializable
  81. {
  82.     public float x = 0.0f;
  83.     public float y = 0.0f;
  84.    
  85.     public String username = "";
  86.    
  87.    
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement