-
Notifications
You must be signed in to change notification settings - Fork 0
/
8B-Server-Client-Socket
122 lines (110 loc) · 2.3 KB
/
8B-Server-Client-Socket
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Server.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Server {
public static void main(String args[])
{
ServerSocket server = null;
try
{
//System.out.println("Enter the port number: ");
//int port;
//Scanner sc = new Scanner(System.in);
//port = sc.nextInt();
server = new ServerSocket(Integer.parseInt(args[0]));
}
catch(Exception e)
{
System.out.println(e);
}
while(true)
{
Socket client = null;
DataInputStream input = null;
PrintStream output = null;
try
{
client = server.accept();
input = new DataInputStream(client.getInputStream());
output = new PrintStream(client.getOutputStream());
}
catch(Exception e)
{
System.out.println(e);
}
output.println("Enter file name :");
try
{
String filename = input.readLine();
System.out.println("Client has requested file: " + filename);
try
{
File f = new File(filename);
BufferedReader bf = new BufferedReader(new FileReader(f));
String data;
while((data = bf.readLine()) != null)
{
output.println(data);
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
//Client.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client {
public static void main(String args[])
{
Socket client = null;
BufferedReader bf = null;
Scanner sc = new Scanner(System.in);
try
{
// String ip = sc.nextLine();
// int port = sc.nextInt();
client = new Socket(args[0],Integer.parseInt(args[1]));
}
catch(Exception e)
{
System.out.println(e);
}
DataInputStream input = null;
PrintStream output = null;
try
{
input = new DataInputStream(client.getInputStream());
output = new PrintStream(client.getOutputStream());
bf = new BufferedReader(new InputStreamReader(System.in));
String str = input.readLine();
System.out.println(str);
String filename = bf.readLine();
if(filename != null)
{
output.println(filename);
}
String data;
while((data = input.readLine()) != null)
{
System.out.println(data);
}
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}