forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherDataServer.java
More file actions
49 lines (40 loc) · 1.66 KB
/
WeatherDataServer.java
File metadata and controls
49 lines (40 loc) · 1.66 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
// filename: WeatherDataServer.java
// Compile: javac WeatherDataServer.java
// Run: java WeatherDataServer
import java.io.*;
import java.net.*;
import java.util.*;
public class WeatherDataServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(5050);
System.out.println("🌦 Weather Server started on port 5050");
System.out.println("Waiting for client connection...");
Socket socket = server.accept();
System.out.println("Client connected successfully!");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Welcome to the Weather Data Server!");
out.println("Type a city name to get current temperature (dummy data). Type 'exit' to quit.");
Map<String, Integer> weatherData = new HashMap<>();
weatherData.put("Delhi", 31);
weatherData.put("Mumbai", 29);
weatherData.put("Bangalore", 27);
weatherData.put("Chennai", 33);
weatherData.put("Kolkata", 30);
String city;
while ((city = in.readLine()) != null) {
if (city.equalsIgnoreCase("exit")) {
out.println("Goodbye!");
break;
}
if (weatherData.containsKey(city)) {
out.println("Temperature in " + city + ": " + weatherData.get(city) + "°C");
} else {
out.println("City not found in database.");
}
}
socket.close();
server.close();
System.out.println("Server closed.");
}
}