-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcpClient.java
34 lines (28 loc) · 1.07 KB
/
tcpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.net.*;
import java.io.*;
class tcpClient
{
public static void main(String args[]) throws IOException
{
//Establish a connection at the given IP and port number. "localhost" refers to this computer
Socket sock=new Socket("localhost",5050);
//Input the file name to be retrived from the server
System.out.println("Enter filename :");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String fname=br.readLine();
//create output stream to send data to outstream
OutputStream ostream=sock.getOutputStream();
PrintWriter pw = new PrintWriter(ostream,true);//StreamToWriteTo , autoFlush
//send filename to the server by writing to outstream using PrintWriter
pw.println(fname);
//Setup input strem to recieve file contents from the server
InputStream istream=sock.getInputStream();
BufferedReader sockread=new BufferedReader(new InputStreamReader(istream));
String msg;
//Store and print out each line that was read from the inputstream
while((msg=sockread.readLine())!=null)
{
System.out.println(msg);
}
}
}