C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,007 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static ServerSocket socket = null;
public static void main(String[] args) {
try {
socket = new ServerSocket(5463);
while(true){
Socket client = socket.accept();
ClientThread clientThread = new ClientThread(client);
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Thread {
private String clientName;
public Client(String name){
this.clientName = name;
}
@Override
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 5463);
OutputStream out = socket.getOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(out);
InputStream in = socket.getInputStream();
ObjectInputStream objectIn = new ObjectInputStream(in);
//connect
objectOut.writeUTF("connect");
objectOut.flush();
//send data
Data xmlData = new Data();
objectOut.writeObject(xmlData);
objectOut.flush();
System.out.println(objectIn.readUTF());
//disconnect
objectOut.writeUTF("disconnect");
objectOut.flush();
System.out.println(objectIn.readUTF());
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
public class ClientThread extends Thread {
private Socket socket;
public ClientThread(Socket serverSocket) {
this.socket = serverSocket;
}
@Override
public void run() {
try {
OutputStream out = socket.getOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(out);
InputStream in = socket.getInputStream();
ObjectInputStream objectIn = new ObjectInputStream(in);
String s;
while (!(s = objectIn.readUTF()).equals("disconnect")) {
if (s.equals("connect")) {
System.out.println(s);
Data data = (Data) objectIn.readObject();
printCGI(data);
objectOut.writeUTF("server received data");
objectOut.flush();
}
}
System.out.println(s);
objectOut.writeUTF("u got disconnected");
objectOut.flush();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
private void printCGI(Data data) {
System.out.println("Content-Type: text/html \r\n");
System.out.println("<html>");
System.out.println("<head><title>Hello World</title></head>");
System.out.println("<body>");
data.getAllEmployee().forEach(employee -> {
System.out.println("<p>" + employee.getId() + "</p>");
System.out.println("<p>" + employee.getFirstname() + "</p>");
System.out.println("<p>" + employee.getLastname() + "</p>");
System.out.println("<p>" + employee.getLocation() + "</p>");
System.out.println("<br>");
});
System.out.println("</body>");
System.out.println("</html>");
}
}
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
public class Data implements Serializable {
private String filename = "employees.xml";
private ArrayList<Employee> allEmployee;
public ArrayList<Employee> getAllEmployee() {
return allEmployee;
}
public Data() {
this.allEmployee = new ArrayList<>();
getXmlData();
System.out.println();
}
public void getXmlData() {
Document document = createDocument();
/*NodeList idList = document.getElementsByTagName("employee");
NodeList firstnameList = document.getElementsByTagName("firstName");
NodeList lastnameList = document.getElementsByTagName("lastName");
NodeList locationList = document.getElementsByTagName("location");
for(int i = 0; i < idList.getLength(); i++){
Employee e = new Employee(idList.item(i).getAttributes().getNamedItem("id").getTextContent(),firstnameList.item(i).getTextContent(),lastnameList.item(i).getTextContent(),locationList.item(i).getTextContent());
allEmployee.add(e);
}
System.out.println();*/
NodeList nodeList = document.getElementsByTagName("employee");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
//get all employee details
Element element = (Element) node;
String id = element.getAttribute("id");
String firstname = element.getElementsByTagName("firstName").item(0).getTextContent();
String lastname = element.getElementsByTagName("lastName").item(0).getTextContent();
String location = element.getElementsByTagName("location").item(0).getTextContent();
//add employee to list
Employee e = new Employee(id, firstname, lastname, location);
allEmployee.add(e);
}
}
public Document createDocument() {
try {
File file = new File(filename);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
return document;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import java.io.Serializable;
public class Employee implements Serializable {
private String id;
private String firstname;
private String lastname;
private String location;
public Employee(String id, String firstname, String lastname, String location) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.location = location;
}
public String getId() {
return id;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public String getLocation() {
return location;
}
}
public class Main {
public static void main(String[] args) {
Client client = new Client("test");
client.start();
}
}
Hi
Java Language Conversion Assistant is a tool that automatically converts existing Java-language code into Visual C#®
Download Tools from Link Address :
https://www.microsoft.com/en-us/download/details.aspx?id=14349
Best Regards.
Please click the Mark as answer button and vote as helpful if this reply solves your problem.