Reputation: 179
I have two questions in java socket programming. this will be stand alone application and will be built only in J2SE.
1) Is it possible to read/write simultaneously via same port in my case since it will be a https request so port will be 443.
2) Is it possible to create two socket connections in one java application. of which one socket act as client and other as server.
I have been looking for some materials regarding this, But i couldn't find any thing useful.
Upvotes: 1
Views: 712
Reputation: 673
For those who are still looking for further explanation. Here is a link to some examples of simple games made using Java Sockets. I find it helpful to have some code to dissect and play around with.
http://cs.lmu.edu/~ray/notes/javanetexamples/
Upvotes: 0
Reputation: 3450
The program that is creating port is a server program.
in a server program you can create multiple ports that listen for client request.
Client doesn't create port only server program does. Client only sends request to server at that server-port. So any number of ports in a program are always server-ports.
When client sends request to server, the server gets a buffer memory where request is places and server reads it. Server also gets another buffer memory where server can write its response that is needed to send back to client. So, yes server can read and write at same time.
Upvotes: 0
Reputation: 2116
A socket connection is two way so you can read and write on one connection. Its similar to connecting a wire plug in socket hence the name socket.
Heres how you do it
Socket socket = new Socket("10.0.0.1", 1234);
OutputStream os = socket.getOutputStream();
InputStream is socket.getInputStream();
new MyInputServiceThread(is).start();
now you can write from os and read from os. You can do it on same thread or on different threads if you expect them not to be in sync.
On 2 you can have any number of clients and server sockets in one app. At least theoritically. There are practical limits. For server sockets you can accept a connection and then spawn a thread passing on the open socket and then your server socket should be ready to accept more connections. In other words to allow multiple connections on the same port you should ensure you do not block after accepting a connection. However you can open more than one server sockets as well in multiple threads.
heres an example
ServerSocket server = new ServerSocket(1234);
while (true) {
Socket socket = server.accept();
// Once it spawns the thread that socket connection is serviced by
//the thread and the
//server socket is ready to accept new connections.
new Mythread(socket).start();
// above Mythread extends Thread....
}
For app as client there is no limit. i.e. as many as you want to connect.
On another note... For https you would also have to accept certificates which means you will have to deal with Private Public keys. Do you really want to do that? as tomcat and other app servers already do that. If this is going to be a web app you would also need to think about a properly signed digital cert. If its intranet then browsers used to access it would have to import a self generated self signed certificate.
Upvotes: 4
Reputation: 19443
To both of your questions the answer is yes. For the second question you will need to create a thread that listens for activity on the server.
And have a look at this tutorial
Upvotes: 2