Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.DataInputStream;
- import java.io.EOFException;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.util.Scanner;
- class MyServer
- {
- // Your program begins with a call to main().
- // Prints "Hello, World" to the terminal window.
- MyServer(int port)
- {
- Scanner scanner = new Scanner(System.in);
- try {
- // System.out.println("Hello, World");
- ServerSocket server = new ServerSocket(port);
- System.out.print("Username is: ");
- String userName = scanner.nextLine();
- // String sentname;
- System.out.println("Waiting to connect...");
- Socket s = server.accept();
- // System.out.println("Username is: " + userName);
- System.out.println("Connected");
- // System.out.println("Connected");
- DataInputStream incoming = new DataInputStream(s.getInputStream());
- DataOutputStream outgoing = new DataOutputStream(s.getOutputStream());
- outgoing.writeUTF(userName);
- outgoing.flush();
- String sentname = incoming.readUTF();
- while(true)
- {
- String send, received;
- //THIS NEEDS TO BE TAKEN INPUT
- send = scanner.nextLine();
- // CLOSE BUTTON PRESS
- if ( send.equals("close"))
- {
- System.out.println("Server Closed");
- incoming.close();
- outgoing.close();
- s.close();
- }
- outgoing.writeUTF(send);
- outgoing.flush();
- // THIS NEEDS TO BE SEEN ON THE SCREEN
- System.out.println(userName + ": " + send);
- received = incoming.readUTF();
- // THIS NEEDS TO BE SEEN ON THE SCREEN
- System.out.println(sentname + ": " + received);
- // if()
- }
- // outgoing.flush();
- // System.out.println("Hello");
- } catch(Exception e){}
- }
- public static void main(String args[])
- {
- MyServer s = new MyServer( (Integer.parseInt(args[0]) ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement