Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the server program
- // You should run this program prior to the client program in a seperate terminal window.
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ChatClient {
- public ChatClient() {
- Socket server;
- DataInputStream receive;
- PrintStream send;
- Scanner read;
- String message;
- try {
- server = new Socket("127.0.0.1", 3000);
- System.out.println("Status: Connected to Server");
- receive = new DataInputStream( new BufferedInputStream(server.getInputStream()) );
- send = new PrintStream( server.getOutputStream() );
- read = new Scanner(System.in);
- while(true) {
- System.out.print("Client: ");
- message = read.nextLine();
- send.println(message);
- System.out.print("Server: ");
- message = receive.readLine();
- System.out.println(message);
- if (message.equals("bye")) {
- send.println("bye");
- break;
- }
- }
- server.close();
- receive.close();
- send.close();
- read.close();
- } catch(Exception exp) {
- System.out.println(exp);
- }
- }
- public static void main(String args[]) {
- ChatClient server = new ChatClient();
- }
- }
Add Comment
Please, Sign In to add comment