Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A Java program for a Client
- import java.net.*;
- import java.io.*;
- public class Client1
- {
- // initialize socket and input output streams
- private Socket socket = null;
- private DataInputStream input = null;
- private DataOutputStream out = null;
- // constructor to put ip address and port
- public Client1(String address, int port)
- {
- // establish a connection
- try
- {
- socket = new Socket(address, port);
- System.out.println("Connected");
- // takes input from terminal
- input = new DataInputStream(System.in);
- // sends output to the socket
- out = new DataOutputStream(socket.getOutputStream());
- }
- catch(UnknownHostException u)
- {
- System.out.println(u);
- }
- catch(IOException i)
- {
- System.out.println(i);
- }
- // string to read message from input
- String line = "";
- // keep reading until "Over" is input
- while (!line.equals("Over"))
- {
- try
- {
- line = input.readLine();
- out.writeUTF(line);
- //System.out.println(line);
- }
- catch(IOException i)
- {
- System.out.println(i);
- }
- }
- // close the connection
- try
- {
- input.close();
- out.close();
- socket.close();
- }
- catch(IOException i)
- {
- System.out.println(i);
- }
- }
- public static void main(String args[])
- {
- Client1 client = new Client1("127.0.0.1", 5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement