question

steakisSIforme-4628 avatar image
0 Votes"
steakisSIforme-4628 asked TimonYang-MSFT edited

Make my code to c# and is this c# code correct?

 JAVA:
    
    
 public class WorkThread extends Thread{
     public Socket workSocket = null;
     public WorkThread(Socket s) { workSocket = s; }
    
     @Override
     public void run() {
         try {
    
             InputStream in = workSocket.getInputStream();
             ObjectInputStream objectIn = new ObjectInputStream(in);
    
    
             XML list;
             String talk;
             while((talk = objectIn.readUTF()).equals("connect")){
                 list = (XML) objectIn.readObject();
                 print(list);
             }
    
    
    
         } catch (IOException | ClassNotFoundException e) {
             e.printStackTrace();
         }
     }
     private void print(XML list){
         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>");
    
         for (Employee e : list.getArrayList()){
             System.out.println("<p>ID: "+e.getId()+"</p><br>");
             System.out.println("<p>Vorname: "+e.getFirstname()+"</p><br>");
             System.out.println("<p>Nachname: "+e.getLastname()+"</p><br>");
             System.out.println("<p>Location: "+e.getLocation()+"</p><br>");
         }
         System.out.println("</body>");
         System.out.println("</html>");
     }
 }
 --------------------------------------------
 --------------------------------------------
    
 public class ClientThread extends Thread {
     @Override
     public void run() {
         try {
             Socket clientSocket = new Socket("127.0.0.1",1111);
    
             OutputStream out = clientSocket.getOutputStream();
             ObjectOutputStream objectOut = new ObjectOutputStream(out);
    
             XML list = new XML();
    
             objectOut.writeUTF("connect");
             objectOut.flush();
             objectOut.writeObject(list);
             objectOut.flush();
    
             objectOut.writeUTF("disconnect");
             objectOut.flush();
    
    
             clientSocket.close();
    
         } catch (IOException | SAXException | ParserConfigurationException e) {
             e.printStackTrace();
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class StartClient {
     public static void main(String[] args){
         ClientThread ct = new ClientThread();
         ct.start();
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class Server {
     public static void runServer() {
         ServerSocket server;
         try {
             server = new ServerSocket(1111); // maximal 65536
             while (true) {
                 Socket client = server.accept();
                 WorkThread wt = new WorkThread(client);
                 wt.start();
             }
         }
         catch (IOException ex) { System.out.println(ex);}
     }
     public static void main(String[] args){
         runServer();
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class Website {
     private ArrayList<Handy> arrayList;
    
     public Website() throws IOException {
         this.arrayList = new ArrayList<Handy>();
         getData();
     }
    
     public void getData() throws IOException {
         URL url = new URL("https://geizhals.at/?fs=handy&hloc=at&in=");
         Document doc = Jsoup.parse(url,10000);
         Elements elements = doc.select("div.listview__content");
    
         for(Element element : elements){
             Elements name = element.select("h3.listview__name");
             Elements preis = element.select("span.price");
             arrayList.add(new Handy(name.text().trim(),preis.text().trim()));
    
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class XML implements Serializable {
     private ArrayList<Employee> arrayList;
    
     public ArrayList<Employee> getArrayList() {
         return arrayList;
     }
    
     public XML() throws IOException, SAXException, ParserConfigurationException {
         this.arrayList = new ArrayList<Employee>();
         getData();
     }
    
     private void getData() throws ParserConfigurationException, IOException, SAXException {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
    
         Document document = builder.parse(new File("employees.xml"));
         document.getDocumentElement().normalize();
    
    
         NodeList nList = document.getElementsByTagName("employee");
         for (int i=0; i<nList.getLength(); i++)
         {
             Node node = nList.item(i);
             if(node.getNodeType() == Node.ELEMENT_NODE){
                 Element item = (Element) node;
                 int id = Integer.parseInt(item.getAttribute("id"));
                 String firstname = item.getElementsByTagName("firstName").item(0).getTextContent();
                 String lastname = item.getElementsByTagName("lastName").item(0).getTextContent();
                 String location = item.getElementsByTagName("location").item(0).getTextContent();
                 arrayList.add(new Employee(id, firstname, lastname, location));
             }
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class Handy {
     private String name;
     private String preis;
    
     public Handy(String name, String preis) {
         this.name = name;
         this.preis = preis;
     }
    
     public Handy() {
    
     }
    
     public String getName() {
         return name;
     }
    
     public String getPreis() {
         return preis;
     }
    
    
 }
    
 --------------------------------------------
 --------------------------------------------
    
 public class Employee implements Serializable {
     private int id;
     private String firstname;
     private String lastname;
     private String location;
    
     public Employee(int id, String firstname, String lastname, String location) {
         this.id = id;
         this.firstname = firstname;
         this.lastname = lastname;
         this.location = location;
     }
    
     public int getId() {
         return id;
     }
    
     public String getFirstname() {
         return firstname;
     }
    
     public String getLastname() {
         return lastname;
     }
    
     public String getLocation() {
         return location;
     }
 }
    
 --------------------------------------------
 --------------------------------------------
 --------------------------------------------

 public class CGI {
    
     public static void main(String[] args) throws IOException {
      String query = System.getenv("QUERY_STRING");
      if(query.contains("search=")){
          cgirun(query);
         }else {
             cgisearch();
         }
     }
     private static void cgirun(String query) throws IOException {
         System.out.println("Content-Type: text/html \r\n");
         System.out.println("<html>");
         System.out.println("<head>");
         System.out.println("<title>CGI-Ergebnis</title></title>");
         System.out.println("</head>");
    
         System.out.println("<body>");
         String product = query.split("=")[1];
         System.out.println("<h1>Dein Produkt ist: "+product.replace("+"," ")+"</h1>");
    
         Document doc = doc = Jsoup.connect("https://www.amazon.com/s?k="+product.replaceAll(" ", "+")).get();
         Elements elements = doc.select("span div.sg-row");
    
         System.out.println("<table style=\"width:100%\">");
    
    
         for (Element element : elements){
             Elements name = element.select("div.a-section > h2 > a");
             Elements price1 = element.select("span.a-price-whole");
             Elements price2 = element.select("span.a-price-fraction");
             if(name.size() == 1 && price1.size() == 1 && price2.size() == 1){
                 System.out.println("<tr>");
                 System.out.println("<td><a href=\"https://www.amazon.com/"+name.attr("href").trim()+"\">"+name.text().trim()+"</a></td>");
                 System.out.println("<td>"+price1.text().trim()+price2.text().trim()+" $</td>");
                 System.out.println("</tr>");
             }
         }
         System.out.println("</table>");
         System.out.println("</body>");
         System.out.println("</html>");
     }
    
     public static Elements getElements(Elements elements, String ... tags){
         Elements e = elements;
         for (String tag : tags) {
             e = e.select(tag);
         }
         return e;
     }
     private static void cgisearch(){
         System.out.println("Content-Type: text/html \r\n");
         System.out.println("<html>");
         System.out.println("<head>");
         System.out.println("<title>CGI-Suche</title></title>");
         System.out.println("</head>");
    
         System.out.println("<body>");
         System.out.println("<form method=\"GET\" action=\"?\">");
    
         System.out.println("<label>Suche: </label>");
         System.out.println("<input type=\"text\" name=\"search\">");
         System.out.println("<br>");
         System.out.println("<input type =\"submit\" value=\"suchen...\">");
    
         System.out.println("</body>");
         System.out.println("</html>");
     }
 }
    
 --------------------------------------------
 --------------------------------------------
 --------------------------------------------
 --------------------------------------------
    
 C#:
    
    
 namespace Reflection
 {
     public partial class Form1 : Form
     {
         protected string filePath;
    
         public Form1()
         {
             InitializeComponent();
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             OpenFileDialog dlg = new OpenFileDialog();
             dlg.Filter = "dll files (*.dll)|*.dll";
             dlg.ShowDialog();
             filePath = dlg.FileName;
    
             ReflectAssembly(filePath);
         }
         private void ReflectAssembly(string filePath)
         {
             Assembly assembly = Assembly.LoadFile(filePath);
             Type[] types = assembly.GetTypes();
    
             foreach (Type type in types)
             {
                 var treeView = new TreeNode(type.Name);
                 MethodInfo[] methods = type.GetMethods();//Methode holen
    
                 foreach (var method in methods)
                 {
                     ParameterInfo[] parameters = method.GetParameters();//Parameter holen
    
                     string retType = method.ReturnType.Name;//Rückgabewert
                     string temp = "\t" + retType + " " + method.Name + "(";
    
                     foreach (var parameter in parameters)
                     {
                         temp += parameter.ParameterType.Name + " " + parameter.Name + ",";
                     }
                     temp += temp.Substring(0, temp.Length - 1);
                     temp += ")";
                     treeView.Nodes.Add(temp);
                 }
                 View.Nodes.Add(treeView);
             }
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 namespace Acrobat
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         private void Form1_Load(object sender, EventArgs e)
         {
    
         }
    
         private void open_Click(object sender, EventArgs e)
         {
             OpenFileDialog dlg = new OpenFileDialog();
             // set file filter of dialog
             dlg.Filter = "pdf files (.pdf) |.pdf;";
             dlg.ShowDialog();
             if (dlg.FileName != null)
             {
                 // use the LoadFile(ByVal fileName As String) function for open the pdf in control
                 axAcroPDF1.LoadFile(dlg.FileName);
             }
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------
    
 namespace ADOBSP_mitXML
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         private void Form1_Load(object sender, EventArgs e)
         {
             // TODO: Diese Codezeile lädt Daten in die Tabelle "aDO_TestDataSet.Test". Sie können sie bei Bedarf verschieben oder entfernen.
            // this.sqlDataAdapter1.Fill(this.bsp1.Test);
    
         }
    
         private void fillByToolStripButton_Click(object sender, EventArgs e)
         {
             try
             {
                 this.testTableAdapter.FillBy(this.aDO_TestDataSet.Test);
             }
             catch (System.Exception ex)
             {
                 System.Windows.Forms.MessageBox.Show(ex.Message);
             }
    
         }
    
         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
         {
             this.testTableAdapter.Fill(this.aDO_TestDataSet.Test, comboBox1.Text);
    
         }
    
         private void button2_Click(object sender, EventArgs e)
         {
             SaveFileDialog sfd = new SaveFileDialog();
             sfd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
             sfd.ShowDialog();
             aDO_TestDataSet.WriteXml(sfd.FileName, XmlWriteMode.WriteSchema);
         }
    
         private void button3_Click(object sender, EventArgs e)
         {
             bsp1.Clear();
             this.sqlDataAdapter1.Fill(this.bsp1.Test);
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
     
             if (ofd.ShowDialog() == DialogResult.OK) {
                 aDO_TestDataSet.Clear();
                 aDO_TestDataSet.ReadXml(ofd.FileName);
             }
         }
     }
 }
    
 --------------------------------------------
 --------------------------------------------

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@steakisSIforme-4628
Have you tried running these codes? I think we'd better start discussions based on certain issues. Some issues may be difficult to see, let alone runtime exceptions.

0 Votes 0 ·

1 Answer

steakisSIforme-4628 avatar image
0 Votes"
steakisSIforme-4628 answered TimonYang-MSFT commented

(THIS IS JUST A TEST!):



Kodierung: UTF-8
Daten werden mit JSON strukturiert übertragen
Port: 1111
Standard-Argument:
method: Die Operation die ausgeführt werden sollte

Rückgabewert
Code: == 0 -> Alles erfolgreich
!= 0 -> Fehler: Text in CodeText
<Optional> Result: Rückgabewert
<Optional> message: Rückgabe-nachricht

Methoden:
register [Registriert die Station]
name: String
uuid: long
sendData [Sendet die Daten]
timestamp: long
value: double
logout [Meldet sich ab -> Gibt den Durchschnitt zurück]

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@steakisSIforme-4628
Is this the solution to your problem?
Besides, this is an English forum, could you please translate it into English and edit your answer?

0 Votes 0 ·