Arianule
Arianule

Reputation: 9043

Socket connection on the client side

I have a questions that is perhaps indicative of my lack in experience and the fact that I am still a student.

I established a socket connection client side(server is already running) and after making the connection on the client side I immediately go to a different Form(that is also based on the client side) where I want to verify userName and password against database on the server side. Problem is, I feel that I do not want to make the connection again as I have already done this on the previous Form

clientSocket = new Socket(hostAdress, 7777);

How can I 'carry over' the fact that I have a connection already to the new form so that I just create and input and output stream without making the connection again on the new form.

Sorry, hope this question makes sense

Kind regards

Arian

Upvotes: 1

Views: 217

Answers (2)

Palejandro
Palejandro

Reputation: 2341

or static variable:

private static Socket clientSocket = new Socket(hostAdress, 7777);

and as Binyamin wrote, create a method , but in this case it would be static method

Upvotes: 1

MByD
MByD

Reputation: 137272

Create a method like this:

public Socket getSocket() {
    return clientSocket;
}

and call it from the other class (assuming that you have a reference to that object.

Upvotes: 4

Related Questions