-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
122 lines (114 loc) · 3.14 KB
/
Client.java
File metadata and controls
122 lines (114 loc) · 3.14 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.*;
import java.net.*;
import java.util.Scanner;
//class Client to send/receive messages to-from the Server
public class Client
{
public static Scanner sc = new Scanner(System.in);
//Method read_message to read the messages received from Server
public static Thread read_message(DataInputStream dis)
{
Thread readFromServer = new Thread(new Runnable()
{
@Override
public void run()
{
String msg = "";
while (!msg.equals("Exit"))
{
try
{
// Read Message from Server
msg = dis.readUTF();
System.out.println("\nServer : "+msg + "\n");
// To check the disconnection request
if(msg.equals("Exit"))
{
//to stop the thread execution on receiving Exit from Server
Thread.currentThread().interrupt();
//System.exit(0);
break;
}
}
catch (IOException e)
{
//e.printStackTrace();
}
}
}
});
return readFromServer;
}
//Method send_message to send messages to Server
public static Thread send_message(DataOutputStream dos,Scanner scn)
{
Thread sendToServer = new Thread(new Runnable()
{
@Override
public void run()
{
String msg = "";
while (!msg.equals("Exit"))
{
msg = scn.nextLine();
try
{
// Write Message to Server
while(msg.length()==0)
{
System.out.println("Please enter something to send.");
msg = scn.nextLine();
}
if(msg.equals("Exit"))
{
dos.writeUTF(msg);
//to stop the thread execution on entering Exit
Thread.currentThread().interrupt();
break;
}
//System.out.println("\nClient : ");
dos.writeUTF(msg);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
return sendToServer;
}
public static void main(String args[]) throws UnknownHostException, IOException
{
Scanner scn = new Scanner(System.in);
//Reading Input regarding IP & Port of server to connect with it
System.out.println("Enter Server IP : ");
String ip = sc.nextLine();
System.out.println("Enter Server Port : ");
int port = sc.nextInt();
// establish the connection
Socket s = new Socket(ip, port);
if(s.isConnected())
System.out.println("Connection Established !! ");
else
System.out.println("Connection not Established !! ");
System.out.println("Enter Message to send: ");
// To Read and Write data to/from other client
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//Multithreading to continuously send and receive messages to/from Server
Thread readFromServer = read_message(dis);
Thread sendToServer = send_message(dos,scn);
sendToServer.start();
readFromServer.start();
while(true)
{
if(!readFromServer.isAlive() || !sendToServer.isAlive())
{
//to close the connection and exit from the program
s.close();
System.exit(0);
}
}
}
}