11,570 questions
Please take a look at the code below, I converted most of the code, but the following points need attention.
- The Thread class cannot be inherited in C#, it is sealed, just like final in Java.
- There is no syntax similar to throws in c#, you need to write try-catch to handle exceptions.
- Socket in Java has InputStream and OutputStream, but in TcpClient of C#, it is replaced by NetworkStream obtained by TcpClient.GetStream Method.
class Program { static void Main(string[] args) { List<Employee> emplist = new List<Employee>(); parseData(emplist); //testOutput(emplist); //sendData(emplist); } public static void parseData(List<Employee> emplist) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(@"D:\test\xml\test.txt"); XmlNodeList xmlLst = xDoc.SelectNodes("root/note"); foreach (XmlNode item in xmlLst) { Console.WriteLine("Name: " + item.SelectNodes("firstName")[0].InnerText); var re = int.Parse(item.Attributes["id"].Value); emplist.Add(new Employee(int.Parse(item.Attributes[""].Value), item.SelectNodes("firstName")[0].InnerText, item.SelectNodes("lastName")[0].InnerText, item.SelectNodes("location")[0].InnerText)); } } public static void testOutput(List<Employee> emplist) { foreach (var item in emplist) { Console.WriteLine(item); } } public static void sendData(List<Employee> emplist) { try { List<Employee> list = emplist; TcpClient clientSocket = new TcpClient("127.0.0.1", 1111); NetworkStream outStrean = clientSocket.GetStream(); var binFormatter = new BinaryFormatter(); var mStream = new MemoryStream(); binFormatter.Serialize(mStream, list); byte[] bytes = mStream.ToArray(); outStrean.Write(bytes, 0, bytes.Length); } catch (IOException) { throw; } } } public class EmployeeServer { public static TcpListener tcpListener; public static void main(String[] args) { IPAddress lServerIp = IPAddress.Parse("192.168.0.196"); tcpListener = new TcpListener(lServerIp, 1111); while (true) { TcpClient tcpClient = tcpListener.AcceptTcpClient(); ServerThread server = new ServerThread(tcpClient); server.run(); } } } public class ServerThread { TcpClient serversocket = null; public ServerThread(TcpClient s) { serversocket = s; } public void run() { try { NetworkStream inStream = serversocket.GetStream(); byte[] myReadBuffer = new byte[serversocket.ReceiveBufferSize]; inStream.Read(myReadBuffer, 0, myReadBuffer.Length); var mStream = new MemoryStream(); var binFormatter = new BinaryFormatter(); // Where 'objectBytes' is your byte array. mStream.Write(myReadBuffer, 0, myReadBuffer.Length); mStream.Position = 0; List<Employee> myObject = binFormatter.Deserialize(mStream) as List<Employee>; foreach (var item in myObject) { Console.WriteLine("firstname: " + item.FirstName + "lastname: " + item.LastName); } } catch (IOException) { } } } public class Employee : ISerializable { private int id; private String firstName; private String lastName; private String location; public int ID { get { return id; } set { this.id = value; } } public string FirstName { get { return firstName; } set { this.firstName = value; } } public string LastName { get { return lastName; } set { this.lastName = value; } } public String Location { get { return location; } set { this.location = value; } } public Employee(int id, String firstName, String lastName, String location) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.location = location; } public override string ToString() { return "Employee{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", location='" + location + '\'' + '}'; } public void GetObjectData(SerializationInfo info, StreamingContext context) { throw new NotImplementedException(); } }
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.