Untitled

 avatar
unknown
java
3 years ago
1.3 kB
6
Indexable
package ie.gmit.dip;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;

public class Client {
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		Socket socket = null;
		DataOutputStream dout = null;
		try {
			socket = new Socket("localhost", 6666);
			OutputStream outputStream = socket.getOutputStream();
			dout = new DataOutputStream(outputStream);
			DataInputStream dis = new DataInputStream(socket.getInputStream());
			String str = (String) dis.readUTF();
			System.out.println("Server recieved message from client");
			System.out.println("New Message: " + str);
			System.out.println("Enter your message:");
			String name = reader.readLine();
			dout.writeUTF(name);
			System.out.println("Client sent the message to server ");
			dout.flush();
		}
		catch (Exception exe ) {
			exe.printStackTrace();
		}
		
		finally {
			try {
				if (dout != null) {
					dout.close();
				}
				if (socket != null) {
					socket.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
Editor is loading...