Server Client Communication C#

WerVardyGestern 1 Reputation point
2022-02-06T19:42:16.097+00:00

Hello I would need help to Convert this type of Server and Client Comunication from Java to C#. I need this for a school project. What is the fastest way to accomplish my Mission

public class Server {

ServerSocket socket;

public Server() throws IOException {
    socket=new ServerSocket(1234);
}

public void run() throws IOException {

    while(true){
        Socket client = socket.accept();
        ClientThread ct= new ClientThread(client);
        ct.run();
    }
}

}

public class ClientThread {

protected Socket socket;
PrintWriter clientOut;
BufferedReader clientIn;

public ClientThread(Socket socket) throws IOException {
    this.socket = socket;
    clientOut=new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
    clientIn=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}

public void run() throws IOException {
    boolean runWhile =true;
    while(runWhile) {
        senden("Hallo Client");
        String msg = clientIn.readLine();
        if(msg.equals("end")){
            runWhile=false;
        }
        System.out.println(msg);
    }
    socket.close();
}

public void senden(String msg){
    clientOut.println(msg);
    clientOut.flush();
}

}

public class Client {

ArrayList<RegisterEntry> entries=new ArrayList<>();
Socket clientSocket;
PrintWriter clientOut;
BufferedReader clientIn;
Database db = new Database();

public Client() throws IOException {
    clientSocket = new Socket("127.0.0.1" , 1234);
    clientOut= new PrintWriter(new BufferedOutputStream(clientSocket.getOutputStream()));
    clientIn= new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}

public void run() throws IOException, SQLException {
    testing();

}

public void senden(String message){
    clientOut.println(message);
    clientOut.flush();
}

public void testing() throws SQLException, IOException {
    //Nachricht vom Server holen
    String msg= clientIn.readLine();
    System.out.println(msg);
    //Antworten
    String answer = "Hey Server";
    senden(answer);

    //Data probieren
    db.insertData(2, LocalDateTime.now(), "javaInsert", "Test insert in Java", "Baraness");
    RegisterEntry registerEntry1= db.getData(2);


    senden(registerEntry1.toString());
    senden ("end");
}

}

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2022-02-06T21:36:51.893+00:00

    The fastest way would be to take the examples on MSDN and adapt them.

    Here's a socket server:
    https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example

    Here's a socket client:
    https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-client-socket-example

    I'm not sure what the specifics of your Database class implementation are but if you just want to be able to create an object in code & save it in a database where the object's public properties correspond to database table columns then you could use something like Entity Framework:
    https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli

    This walkthrough shows you how to set it up with a file based database called SQLite, so you don't have to worry about setting up a database server to get something working.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.