0% found this document useful (0 votes)
39 views

Write Your Code Here: "Battery - TXT"

This Java code defines a Main class that acts as a server. It opens two server sockets, one on port 5000 and one on port 4050. When a connection is received on the first socket, it starts a new thread to receive a file. It then reads the received file, parses it line by line using a regular expression to extract key-value pairs into a map, and writes the value for the "model" key back to the second connected client.

Uploaded by

Utkarsh Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Write Your Code Here: "Battery - TXT"

This Java code defines a Main class that acts as a server. It opens two server sockets, one on port 5000 and one on port 4050. When a connection is received on the first socket, it starts a new thread to receive a file. It then reads the received file, parses it line by line using a regular expression to extract key-value pairs into a map, and writes the value for the "model" key back to the second connected client.

Uploaded by

Utkarsh Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.

company;
import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws IOException {
// write your code here
BufferedWriter bufferedWriter=null;
int FILE_SIZE=6022386;
String FILE_TO_RECEIVED = "Battery.txt";
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
while (true) {
try {
ServerSocket serverSocket=new ServerSocket(5000);
ServerSocket serverSocket1=new ServerSocket(4050);
Socket socket=serverSocket.accept();
Socket socket1=serverSocket1.accept();
System.out.println("connected "+socket);

new RecieveFile(socket).start();

File file=new File("Battery.txt");


if(file!=null) {
BufferedReader br = new BufferedReader(new
FileReader("Battery.txt"));
PrintWriter pr = new PrintWriter(socket1.getOutputStream(), true);
List<String> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
String sh;
Pattern pattern = Pattern.compile("(.*):(.*)");
while ((sh = br.readLine()) != null) {
list.add(sh.trim());
}
br.close();
for (String i : list) {
Matcher matcher = pattern.matcher(i);
while (matcher.find()) {
map.put(matcher.group(1).trim(), matcher.group(2).trim());
}
}
pr.write(map.get("model"));
// System.out.println(map.get("model"));
pr.close();
// socket.close();
// socket1.close();
}
} catch(IOException b){
b.getMessage();
} catch (NullPointerException e){
}catch (ArrayIndexOutOfBoundsException e){
}
}
}
}

You might also like