-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyServer.java
More file actions
69 lines (59 loc) · 1.4 KB
/
MyServer.java
File metadata and controls
69 lines (59 loc) · 1.4 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
//Server Side Program.
import java.net.*;
import java.io.*;
class MyServer
{
ServerSocket ss;
Socket clientsocket;
BufferedReader fromclient;
InputStreamReader isr;
PrintWriter toclient;
public MyServer()
{
String str = new String("hello");
try
{
// Create ServerSocket object.
ss = new ServerSocket(1234);
System.out.println("Server Started...");
while(true)
{
System.out.println("Waiting for the client...");
clientsocket = ss.accept();
while(true)
{
System.out.println("Waiting for the request...");
// accept the client request.
//clientsocket = ss.accept();
System.out.println("Got a client");
System.out.println("Client Address "+ clientsocket.getInetAddress().toString());
isr = new InputStreamReader(clientsocket.getInputStream());
fromclient = new BufferedReader(isr);
toclient = new PrintWriter(clientsocket.getOutputStream());
str = fromclient.readLine();
System.out.println(str);
toclient.println("Server : Your Message Recieved ");
System.out.println("Response sent to the client " + toclient.checkError());
if(str.equals("bye"))
{
fromclient.close();
toclient.close();
clientsocket.close();
break;
}
}
}
//fromclient.close();
//toclient.close();
//clientsocket.close();
}
catch(Exception ex)
{
System.out.println("Error in the code : " + ex.toString());
}
}
public static void main(String arg[])
{
MyServer serverobj = new MyServer();
}
}