Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- public class Client {
- Socket s = null;
- DataInputStream dis = null;
- DataOutputStream dos = null;
- BufferedReader br = null;
- String address = "localhost";
- int port = 3333;
- public Client() {
- try {
- System.out.println("========== Establishing client connection ==========");
- s = new Socket(address,port);
- dis = new DataInputStream(s.getInputStream());
- dos = new DataOutputStream(s.getOutputStream());
- br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("========== Client connected! ==========");
- String msg="",rcv="";
- while(!msg.equals("stop")) {
- msg = br.readLine();
- System.out.println("You: "+msg);
- rcv = readSocket();
- System.out.println("Server: "+rcv);
- }
- closeClientConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String writeSocket(String message) throws IOException{
- System.out.println("========== Write something. ==========");
- System.out.print("You :");
- dos.writeUTF(message);
- dos.flush();
- return message;
- }
- public String readSocket() throws IOException{
- System.out.println("========== Fetching message... ==========");
- String recieved = dis.readUTF();
- return recieved;
- }
- public void closeClientConnection() throws IOException{
- System.out.println("========== Closing server connection ==========");
- dos.close();
- dis.close();
- s.close();
- }
- }
Add Comment
Please, Sign In to add comment