Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package potikgame;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- public class networking {
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private String message = "";
- private String serverIP;
- private Socket connection;
- public networking(String string) {
- serverIP=string;
- }
- public void startRunning(){
- try{
- connectToServer();
- setupStreams();
- whileChatting();
- }catch(EOFException eofException){
- showMessage("\n Client terminated connection");
- }catch(IOException ioException){
- ioException.printStackTrace();
- }finally{
- closeCrap();
- }
- }
- private void connectToServer() throws IOException{
- showMessage("Attempting connection...hold your horses...\n");
- connection = new Socket(InetAddress.getByName(serverIP), 7328);
- showMessage("Connected to: "+connection.getInetAddress().getHostName() );
- }
- private void setupStreams() throws IOException{
- output = new ObjectOutputStream(connection.getOutputStream());
- output.flush();
- input = new ObjectInputStream(connection.getInputStream());
- showMessage("\n Dude,your shit is now online \n");
- }
- private void whileChatting() throws IOException{
- do{
- try{
- message = (String) input.readObject();
- showMessage("\n" + message);
- }catch(ClassNotFoundException classNotfoundException){
- showMessage("\n Dude,you fuck your shit up \n");
- }
- }while(!message.equals("SERVER - END"));
- }
- private void closeCrap(){
- showMessage("\n Closing crap down...");
- System.out.println("done");
- try{
- output.close();
- input.close();
- connection.close();
- }catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- public void sendMessage(String message){
- try{
- output.writeObject("CLIENT "+connection.getInetAddress().getHostName()+" - "+message);
- output.flush();
- showMessage("\nCLIENT "+connection.getInetAddress().getHostName()+" - "+message);
- }catch(IOException ioException){
- System.out.println("Something went wrong");
- }
- }
- private void showMessage(String string) {
- System.out.println(string);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement