Advertisement
Guest User

networking

a guest
Nov 23rd, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package potikgame;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.net.InetAddress;
  8. import java.net.Socket;
  9.  
  10. public class networking {
  11.     private ObjectOutputStream output;
  12.     private ObjectInputStream input;
  13.     private String message = "";
  14.     private String serverIP;
  15.     private Socket connection;
  16.     public networking(String string) {
  17.         serverIP=string;
  18.     }
  19.     public void startRunning(){
  20.         try{
  21.             connectToServer();
  22.             setupStreams();
  23.             whileChatting();
  24.         }catch(EOFException eofException){
  25.             showMessage("\n Client terminated connection");
  26.         }catch(IOException ioException){
  27.             ioException.printStackTrace();
  28.         }finally{
  29.             closeCrap();
  30.         }
  31.     }
  32.     private void connectToServer() throws IOException{
  33.         showMessage("Attempting connection...hold your horses...\n");
  34.         connection = new Socket(InetAddress.getByName(serverIP), 7328);
  35.         showMessage("Connected to: "+connection.getInetAddress().getHostName() );
  36.     }
  37.     private void setupStreams() throws IOException{
  38.         output = new ObjectOutputStream(connection.getOutputStream());
  39.         output.flush();
  40.         input = new ObjectInputStream(connection.getInputStream());
  41.         showMessage("\n Dude,your shit is now online \n");
  42.     }
  43.     private void whileChatting() throws IOException{
  44.         do{
  45.             try{
  46.                 message = (String) input.readObject();
  47.                 showMessage("\n" + message);
  48.             }catch(ClassNotFoundException classNotfoundException){
  49.                 showMessage("\n Dude,you fuck your shit up \n");
  50.             }
  51.         }while(!message.equals("SERVER - END"));
  52.     }
  53.     private void closeCrap(){
  54.         showMessage("\n Closing crap down...");
  55.         System.out.println("done");
  56.         try{
  57.             output.close();
  58.             input.close();
  59.             connection.close();
  60.         }catch(IOException ioException){
  61.             ioException.printStackTrace();
  62.         }
  63.     }
  64.     public void sendMessage(String message){
  65.         try{
  66.             output.writeObject("CLIENT "+connection.getInetAddress().getHostName()+" - "+message);
  67.             output.flush();
  68.             showMessage("\nCLIENT "+connection.getInetAddress().getHostName()+" - "+message);
  69.         }catch(IOException ioException){
  70.             System.out.println("Something went wrong");
  71.         }
  72.     }
  73.     private void showMessage(String string) {
  74.         System.out.println(string);
  75.        
  76.     }
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement