Java Socket 编程

发布于 2024-12-21 23:36:11 字数 217 浏览 4 评论 0原文

我有两个关于java套接字编程的问题。这将是一个独立的应用程序,并且仅在 J2SE 中构建。

1) 在我的情况下,是否可以通过同一端口同时读/写,因为它将是一个 https 请求,因此端口将为 443。

2) 是否可以在一个 java 应用程序中创建两个套接字连接。其中一个套接字作为客户端,另一个作为服务器。

我一直在寻找一些与此相关的材料,但找不到任何有用的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-28 23:36:11

套接字连接是两种方式,因此您可以在一个连接上进行读写。它类似于将电线插头连接到插座中,因此得名插座。

现在您可以按照以下方式进行操作,

Socket socket = new Socket("10.0.0.1", 1234);
OutputStream os = socket.getOutputStream();
InputStream is socket.getInputStream();
new MyInputServiceThread(is).start();

您可以从操作系统写入并从操作系统读取。如果您希望它们不同步,您可以在同一线程或不同线程上执行此操作。

在 2 上,您可以在一个应用程序中拥有任意数量的客户端和服务器套接字。至少理论上是这样。有实际限制。对于服务器套接字,您可以接受一个连接,然后生成一个在打开的套接字上传递的线程,然后您的服务器套接字应该准备好接受更多连接。换句话说,要允许同一端口上的多个连接,您应该确保在接受连接后不会阻塞。但是,您也可以在多个线程中打开多个服务器套接字。

这是一个示例

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....    
}

对于作为客户端的应用程序,没有限制。即您想连接多少就连接多少。

另一方面……
对于 https,您还必须接受证书,这意味着您必须处理私钥和公钥。你真的想这么做吗?因为 tomcat 和其他应用程序服务器已经这样做了。如果这将是一个网络应用程序,您还需要考虑正确签名的数字证书。如果是内部网,则用于访问它的浏览器必须导入自行生成的自签名证书。

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.

满身野味 2024-12-28 23:36:11

对于你的两个问题,答案都是肯定的。对于第二个问题,您需要创建一个线程来侦听服务器上的活动。

并查看本教程

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

音盲 2024-12-28 23:36:11
  1. 创建端口的程序是一个服务器程序。

  2. 在服务器程序中,您可以创建多个侦听客户端请求的端口。

  3. 客户端不会创建端口,只有服务器程序才会创建。客户端仅在该服务器端口向服务器发送请求。因此,程序中任意数量的端口始终是服务器端口。

  4. 当客户端向服务器发送请求时,服务器会获得一个缓冲区内存,请求放置在该缓冲区中,服务器会读取它。服务器还获得另一个缓冲区内存,服务器可以在其中写入发送回客户端所需的响应。所以,是的,服务器可以同时读写。

  1. The program that is creating port is a server program.

  2. in a server program you can create multiple ports that listen for client request.

  3. 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.

  4. 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.

苏璃陌 2024-12-28 23:36:11

对于那些仍在寻找进一步解释的人。以下是使用 Java 套接字制作的一些简单游戏示例的链接。我发现剖析和使用一些代码很有帮助。

http://cs.lmu.edu/~ray/notes/javanetexamples/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文