Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.Console;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.SocketException;
- import java.util.Scanner;
- public class FileClient {
- public static void main(String[] args) throws Exception {
- // Give error if no server ip and port passed as arguments
- if (args.length != 2) {
- System.err.println("Usage: java FileClient <server address> <port number>");
- System.exit(1);
- }
- String serverName = args[0];
- int serverPort = Integer.parseInt(args[1]);
- try {
- System.out.println("Connecting to " + serverName + " on port " + serverPort);
- Socket client = new Socket(serverName, serverPort);
- System.out.println("Just connected to " + client.getRemoteSocketAddress() + "\n");
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter a command: ");
- String command = scanner.nextLine();
- scanner.close();
- OutputStream outToServer = client.getOutputStream();
- DataOutputStream out = new DataOutputStream(outToServer);
- out.writeUTF(command);
- //out.writeUTF("Hello from " + client.getLocalSocketAddress());
- InputStream inFromServer = client.getInputStream();
- DataInputStream in = new DataInputStream(inFromServer);
- String value;
- while (in.available() > 0 && (value = in.readUTF()) != null) {
- System.out.println(value);
- }
- in.close();
- out.close();
- client.close();
- } catch (SocketException e){
- System.out.println("Error connecting to the server");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement