- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtcpClient.java
More file actions
Latest commit
34 lines (28 loc) · 1.07 KB
/
Copy pathtcpClient.java
File metadata and controls
34 lines (28 loc) · 1.07 KB
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
importjava.net.*;
importjava.io.*;
classtcpClient
{
publicstaticvoidmain(Stringargs[]) throwsIOException
{
//Establish a connection at the given IP and port number. "localhost" refers to this computer
Socketsock=newSocket("localhost",5050);
//Input the file name to be retrived from the server
System.out.println("Enter filename :");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Stringfname=br.readLine();
//create output stream to send data to outstream
OutputStreamostream=sock.getOutputStream();
PrintWriterpw = newPrintWriter(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
InputStreamistream=sock.getInputStream();
BufferedReadersockread=newBufferedReader(newInputStreamReader(istream));
Stringmsg;
//Store and print out each line that was read from the inputstream
while((msg=sockread.readLine())!=null)
{
System.out.println(msg);
}
}
}