implement tcp server for transferring files using socket and serversocket

Solutions on MaxInterview for implement tcp server for transferring files using socket and serversocket by the best coders in the world

showing results for - "implement tcp server for transferring files using socket and serversocket"
Sabrina
17 Jul 2016
1//Client
2import java.io.*;
3import java.net.*;
4
5public class Client {
6
7    public static void main(String[] args) throws Exception {
8        Socket s = new Socket("127.0.0.1", 1234);
9        if (s.isConnected()) {
10            System.out.println("Connected to server");
11        }
12        FileOutputStream fout = new FileOutputStream("received.txt");
13        DataInputStream din = new DataInputStream(s.getInputStream());
14        int r;
15        while ((r = din.read()) != -1) {
16            fout.write((char) r);
17        }
18        s.close();
19    }
20
21}
22
23//Server
24import java.io.*;
25import java.net.*;
26
27class Server {
28    public static void main(String args[]) throws Exception {
29        ServerSocket ss = new ServerSocket(1234);
30        Socket s = ss.accept();
31        System.out.println("connected..........");
32        FileInputStream fin = new FileInputStream("Send.txt");
33        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
34        int r;
35        while ((r = fin.read()) != -1) {
36            dout.write(r);
37
38        }
39        System.out.println("\nFiletranfer Completed");
40        s.close();
41        ss.close();
42    }
43}