user13001788
user13001788

Reputation:

Socket java programming

I'm beginner in Java programming and I have to realize a project with sockets. I'm trying to get the data from the client to connect, but I'm stuck in reading the data from the DAtaInputStream. I don't know why the run method stops executing in the line "op=din.readInt()".

public class ServerWorker extends Thread {

    public final Socket clientSocket;
    Server s;
    BDClass con;
    boolean stillConnecting = true;
    Thread runner;

    public ServerWorker(Server s, Socket clientSocket) throws ClassNotFoundException {
        this.con = new BDClass();
        this.clientSocket = clientSocket;
        this.s = s;
        this.runner = new Thread(this);
        this.runner.start();

    }

    public boolean login(String nom, String pass) throws SQLException, IOException {
        ResultSet User;
        if (nom != "" && pass != "") {
            if (con.Authentifier(nom, pass)) {
                User = con.TrouverUtilisateur(nom);
                new ProfilFenetre(con.getFriends(User.getInt(1)), User).setVisible(true);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    @Override
    public void run() {
        DataInputStream din;
        DataOutputStream dout;
        int  op = 0;
        ConnexionFenetre f;
        try {

            f = new ConnexionFenetre(s, clientSocket);
            f.setVisible(true);

            System.out.print("viide");
            while (true) {

                try {
                    if (true) {
                        din = new DataInputStream(clientSocket.getInputStream());
                        dout = new DataOutputStream(clientSocket.getOutputStream());

                        op = din.readInt();

                        System.out.print(op);

                        switch (op) {
                            case 0: {
                                System.out.println("no request");
                                break;
                            }
                            case 1: {
                                DataInputStream din1 = new DataInputStream(clientSocket.getInputStream());
                                DataInputStream din2 = new DataInputStream(clientSocket.getInputStream());
                                System.out.print(din1.readUTF() + "  llll  " + din2.readUTF());
                                if (this.login(din1.readUTF(), din2.readUTF())) {
                                    f.dispose();
                                    dout.writeUTF("you are connected!!");
                                } else {
                                    dout.writeUTF("mot de passe ou username faux!!");
                                }
                                break;
                            }

                            default: {

                            }

                        }
                    }
                } catch (IOException e) {
                    System.err.print(e.getMessage());
                } catch (SQLException ex) {
                    System.err.print(ex.getMessage());
                    Logger.getLogger(ServerWorker.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        } catch (IOException ex) {
            Logger.getLogger(ServerWorker.class.getName()).log(Level.SEVERE, null, ex);
            System.err.print(ex.getMessage());
        }

    }
}

Upvotes: 0

Views: 114

Answers (2)

Force Bolt
Force Bolt

Reputation: 1221

Socket java programming is used for communication between the applications running on different platform of Java Runtime Environment(JRE).

It is java programming can be connection-oriented or connection-less communication.

Socket are used for DatagramPacket, DatagramSocket and connection-oriented socket programming classes are used for connection-less socketjava programming.

Network communication appear in Transport Control Protocol (TCP) and User Datagram Protocol (UDP). TCP and UDP both have unique ways and it is used for different ways purposes.

Network communication are two types are:-

1). TCP is simple and reliable protocol which enables a client to make a connection to a server and the two systems to communicate. Transport Control Protocol every entity knows that its communication payloads have been received comes from server.

2). UDP is a connectionless protocol and very efficient for some cases where we do not need every packet to arrive at its destination, such as media streaming.

import java.net.*;  
import java.io.*;  

public class ServerData {  
    
public static void main(String[] args){  
    
    try{  
    ServerSocket ss=new ServerSocket(3000);  
    Socket s=ss.accept();//connection orientation   
    DataInputStream dis=new DataInputStream(s.getInputStream());  
    String  str=(String)dis.readUTF();  
    System.out.println("message= "+str);  
    ss.close();  
    }catch(Exception e){
        System.out.println(e);}  
    }  
} 
    
import java.net.*;  
import java.io.*;  

public class UserData { 
    
public static void main(String[] args) {  
    try{      
    Socket s=new Socket("localhost",3000);  
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());  
    dout.writeUTF("Hello Server Data");  
    dout.flush();  
    dout.close();  
    s.close();  
    }catch(Exception e){
        System.out.println(e);}  
    }  
} 

Upvotes: -1

PabloBuendia
PabloBuendia

Reputation: 263

din.readInt();

is a blocking operation, which means the thread executing the method will stop until it gets data to read.

Upvotes: 2

Related Questions