-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
135 lines (126 loc) · 3.22 KB
/
Server.java
File metadata and controls
135 lines (126 loc) · 3.22 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
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.net.*;
import java.io.*;
import java.util.Scanner;
//class Server which will act as Server and accepts connection from client
public class Server
{
//Method read_message reads and prints the message received from client
public static Thread read_message(DataInputStream in)
{
Thread readFromClient = null;
readFromClient = new Thread(new Runnable()
{
@Override
public void run()
{
String line="";
//to read message from client
while(!line.equals("Exit"))
{
try
{
line = in.readUTF();
System.out.println("\nClient : "+line+"\n");
if(line.equals("Exit"))
{
//to stop the thread
Thread.currentThread().interrupt();
//System.exit(0);
break;
}
}
catch(IOException i)
{
System.out.println("Exception : " +i);
}
}
}
});
return readFromClient;
}
//Method send_message sends message to the connected client
public static Thread send_message(DataOutputStream out, Scanner input)
{
Thread sendToClient = null;
sendToClient = new Thread(new Runnable()
{
@Override
public void run()
{
String message="";
//send message to client
while(!message.equals("Exit"))
{
try
{
message = input.nextLine();
//if message is empty
while(message.length()==0)
{
System.out.println("Please enter something to send.");
message = input.nextLine();
}
if(message.equals("Exit"))
{
out.writeUTF(message);
//to stop the thread
Thread.currentThread().interrupt();
//System.exit(0);
break;
}
out.writeUTF(message);
}
catch(IOException i)
{
System.out.println("Exception : " +i);
}
}
}
});
return sendToClient;
}
public static void main(String args[])
{
Socket socket = null;
ServerSocket serverS = null;
DataInputStream din = null;
DataOutputStream dos = null;
Scanner input = null;
//to run server on specified port
int port;
input = new Scanner(System.in);
System.out.println("Enter Port Number on which Server will be running: ");
port = input.nextInt();
try
{
//starting server to wait and accept the connection
serverS = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client...");
socket = serverS.accept();
System.out.println("Client accepted");
System.out.println("Enter message to send: ");
din = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
dos = new DataOutputStream(socket.getOutputStream());
input = new Scanner(System.in);
//Multithreading to continuously send and receive message to/from client
Thread readFromClient = read_message(din);
Thread sendToClient = send_message(dos, input);
readFromClient.start();
sendToClient.start();
while(true)
{
if(!readFromClient.isAlive() || !sendToClient.isAlive())
{
//to close the connection and stop the program
socket.close();
System.exit(0);
}
}
}
catch(IOException i)
{
System.out.println(i);
}
}
}