-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.java
More file actions
99 lines (82 loc) · 2.54 KB
/
Client.java
File metadata and controls
99 lines (82 loc) · 2.54 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
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
/**
* 1)The Client class sends the message to the server.
* 2)The client waits for the message to be received back.
* 3)On receiving the message the client prints the message.
* 4)After printing the client exits.
*/
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
//Creates a socket connection.
socket = new Socket(address, port);
//Creates an output stream object.
OutputStream os = socket.getOutputStream();
//Creates an output stream writer objecrt.
OutputStreamWriter osw = new OutputStreamWriter(os);
//Creates a buffered writer object.
BufferedWriter bw = new BufferedWriter(osw);
//Message to be sent to the server.
String input = "This is my text to be changed by the SERVER ";
String sendMessage = input + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage.trim());
//Receiving message back from the server.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
//Printing the received message from the server.
System.out.println("Message received from the server : " +message);
}
//Catches the socket exception.
catch (SocketException e) {
System.out.println("Error in socket connection");
}
//Catches string exceptions.
catch (StringIndexOutOfBoundsException e) {
System.out.println("Out of range for the received message");
}
//Catches indexing errors.
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index is out of range");
}
//Catches input and output errors.
catch (IOException e) {
System.out.println("Error with sending/receiving");
}
//Catches general exceptions.
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}