[英]Java Socket programming
I have two questions in java socket programming. 我在Java套接字编程中有两个问题。 this will be stand alone application and will be built only in J2SE.
这将是独立的应用程序,仅在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. 1)在我的情况下是否可以通过同一端口同时进行读/写,因为这将是一个https请求,因此端口将是443。
2) Is it possible to create two socket connections in one java application. 2)是否可以在一个Java应用程序中创建两个套接字连接。 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. 我一直在寻找有关此的一些材料,但是我找不到任何有用的东西。
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. 现在您可以从os进行写入和从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. 在2上,您可以在一个应用程序中拥有任意数量的客户端和服务器套接字。 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. 对于作为客户端的应用程序,没有限制。 ie 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. 另一方面...对于https,您还必须接受证书,这意味着您将必须处理私钥。 Do you really want to do that?
您真的要这么做吗? as tomcat and other app servers already do that.
因为tomcat和其他应用服务器已经做到了。 If this is going to be a web app you would also need to think about a properly signed digital cert.
如果这将是一个Web应用程序,则还需要考虑一个经过正确签名的数字证书。 If its intranet then browsers used to access it would have to import a self generated self signed certificate.
如果其Intranet,则用于访问它的浏览器将必须导入一个自行生成的自签名证书。
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 看看这个教程
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.
因此,是的服务器可以同时读写。
For those who are still looking for further explanation. 对于那些仍在寻找进一步解释的人。 Here is a link to some examples of simple games made using Java Sockets.
这是使用Java套接字制作的一些简单游戏示例的链接。 I find it helpful to have some code to dissect and play around with.
我发现分解并使用一些代码很有帮助。
http://cs.lmu.edu/~ray/notes/javanetexamples/ http://cs.lmu.edu/~ray/notes/javanetexamples/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.