热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JavaSocketProgramminginClient/ServerApplications转自http://www.developer.com/

原文出处:http:www.developer.comjavaarticle.php10922_3840466_1Java-Socket-Programming-in-Client

 

原文出处:http://www.developer.com/java/article.php/10922_3840466_1/Java-Socket-Programming-in-ClientServer-Applications.htm

 

In client/server applications, the client and server components usually do not reside on the same computer (i.e., the client could be installed on a computer that is different from the one hosting the server installation); yet logically they are components of the same application. To enable the client and server components to communicate with each other over a network, systems often rely heavily on sockets.

 

Because Java provides the required capabilities for developing socket-based applications relatively easily and hides all the complexity involved, Java developers have an advantage in providing quick solutions to networking problems.

 

The ServerSocket and Socket Classes

Java supports socket development through the java.net package. In particular, ServerSocket and Socket are the socket-related classes. They provide infrastructure for server and client development, respectively. This article explores these classes—and how to use them—in more detail.

 

You use the ServerSocket class on the server component, where it listens for incoming requests. ServerSocket generally is bound to a port, always active, and non-blocking. This means that it immediately transfers or hands over any incoming request to a different component and then continues listening for new incoming requests. 

 

To connect with the server component, the client component simply requests a connection to the computer where it expects the server to be running. It includes the computer name and the port to which the server is bound in this request. The resulting connection is permanent for the client; it will not be shared with any other instance of the client software running on the same machine.

 

Confused? Here is the concept in a nutshell:

 

   1. The server listens on the port to which it is bound and waits for incoming requests.

   2. When it receives a connection request, it creates a socket and associates the new connection to that socket.

   3. From then on, the client will communicate with the server via this newly created socket (although it is not aware of it). 

 

Imagine achieving this setup using native code. It would be a lot of work, and if you weren't aware of the low-level constraints (like many developers), you would end up producing poor quality code. Luckily, the Java socket APIs are well tested and used by a large group of developers, which ensures that all possible hidden issues are caught and addressed. That is why developers using Java can produce high-quality client/server applications based on sockets. 

 

Writing the Server Using Sockets

Code Listing 1 provides a Java example for writing a server component using sockets. In this code, the server binds itself with a ServerSocket on to a pre-determined port. This port will be the one to which clients can request connections. The server then awaits client connection requests on the ServerSocket. When it receives them, it will create a new socket and hand the connection over to it.

 

The code uses the accept() method on the ServerSocket class, designating the socket as the one responsible for communication with the client. All information to and from the client will be sent and received via this socket. The getInputStream() and getOutputStream() methods are used for the communication with the client.

 

Figure 1 shows a screenshot of the server console when it starts. 

 

Figure 1. The Server Console When It Starts:

Here is a screenshot of the server console when it starts.

 

Writing the Client Using Sockets

Code Listing 2 provides a Java example for writing a client component using sockets.

In this code, the client inputs the server name and the port through which it deliver arguments for creating a socket connection. When the socket is created successfully, it can then talk to the server with the input/output streams. The exception handling has to be effective to pinpoint any problems in the connection.

 

Figure 2 shows a screenshot of the server console when the client starts, Figure 3 shows a screenshot of the client console, Figure 4 shows a screenshot of the server console when the client starts communicating, and Figure 5 shows a screenshot of the client console during communication. 

 

Figure 2. Server Console When the Client Starts:

Here is a screenshot of the server console when the client starts.

 

Figure 3. The Client Console:

Here is a screenshot of the client console.

 

Figure 4. The Server Console When the Client Starts Communicating:

Here is a screenshot of the server console when the client starts communicating.

 

Figure 5. The Client Console During Communication:

Here is a screenshot of the client console during communication.

 

As you start using the application, you can think of numerous enhancements to this example.

 

Care and Feeding of Your Applications

It is important to close all open resources, such as file handlers, socket connections, and any other open entities because these will lead to memory-leak issues in the long run.

 

源码:

 

 

 


推荐阅读
  • Python 伦理黑客技术:深入探讨后门攻击(第三部分)
    在《Python 伦理黑客技术:深入探讨后门攻击(第三部分)》中,作者详细分析了后门攻击中的Socket问题。由于TCP协议基于流,难以确定消息批次的结束点,这给后门攻击的实现带来了挑战。为了解决这一问题,文章提出了一系列有效的技术方案,包括使用特定的分隔符和长度前缀,以确保数据包的准确传输和解析。这些方法不仅提高了攻击的隐蔽性和可靠性,还为安全研究人员提供了宝贵的参考。 ... [详细]
  • 公司有个系统,比较古老,web端使用的是applet,applet作为socketclient端。另一台服务器运行socketserver.当多次执行某一个特定事务时(通过Applet,该事务会触 ... [详细]
  • 【重识云原生】第四章云网络4.8.3.2节——Open vSwitch工作原理详解
    2OpenvSwitch架构2.1OVS整体架构ovs-vswitchd:守护程序,实现交换功能,和Linux内核兼容模块一起,实现基于流的交换flow-basedswitchin ... [详细]
  • Spring – Bean Life Cycle
    Spring – Bean Life Cycle ... [详细]
  • 单片微机原理P3:80C51外部拓展系统
      外部拓展其实是个相对来说很好玩的章节,可以真正开始用单片机写程序了,比较重要的是外部存储器拓展,81C55拓展,矩阵键盘,动态显示,DAC和ADC。0.IO接口电路概念与存 ... [详细]
  • Unity与MySQL连接过程中出现的新挑战及解决方案探析 ... [详细]
  • 本文介绍了如何利用Shell脚本高效地部署MHA(MySQL High Availability)高可用集群。通过详细的脚本编写和配置示例,展示了自动化部署过程中的关键步骤和注意事项。该方法不仅简化了集群的部署流程,还提高了系统的稳定性和可用性。 ... [详细]
  • Java Socket 关键参数详解与优化建议
    Java Socket 的 API 虽然被广泛使用,但其关键参数的用途却鲜为人知。本文详细解析了 Java Socket 中的重要参数,如 backlog 参数,它用于控制服务器等待连接请求的队列长度。此外,还探讨了其他参数如 SO_TIMEOUT、SO_REUSEADDR 等的配置方法及其对性能的影响,并提供了优化建议,帮助开发者提升网络通信的稳定性和效率。 ... [详细]
  • 优化后的标题:深入探讨网关安全:将微服务升级为OAuth2资源服务器的最佳实践
    本文深入探讨了如何将微服务升级为OAuth2资源服务器,以订单服务为例,详细介绍了在POM文件中添加 `spring-cloud-starter-oauth2` 依赖,并配置Spring Security以实现对微服务的保护。通过这一过程,不仅增强了系统的安全性,还提高了资源访问的可控性和灵活性。文章还讨论了最佳实践,包括如何配置OAuth2客户端和资源服务器,以及如何处理常见的安全问题和错误。 ... [详细]
  • 本文详细介绍了一种利用 ESP8266 01S 模块构建 Web 服务器的成功实践方案。通过具体的代码示例和详细的步骤说明,帮助读者快速掌握该模块的使用方法。在疫情期间,作者重新审视并研究了这一未被充分利用的模块,最终成功实现了 Web 服务器的功能。本文不仅提供了完整的代码实现,还涵盖了调试过程中遇到的常见问题及其解决方法,为初学者提供了宝贵的参考。 ... [详细]
  • 在Java Web服务开发中,Apache CXF 和 Axis2 是两个广泛使用的框架。CXF 由于其与 Spring 框架的无缝集成能力,以及更简便的部署方式,成为了许多开发者的首选。本文将详细介绍如何使用 CXF 框架进行 Web 服务的开发,包括环境搭建、服务发布和客户端调用等关键步骤,为开发者提供一个全面的实践指南。 ... [详细]
  • 《Intel IA-32 架构软件开发人员手册详尽指南》提供了详尽的 IA-32 架构技术文档,涵盖指令集、系统编程和硬件接口等内容,为软件开发人员提供全面的技术支持和参考。该手册不仅包括详细的架构说明,还提供了丰富的编程示例和最佳实践,帮助开发人员更好地理解和应用 IA-32 架构。 ... [详细]
  • IOS Run loop详解
    为什么80%的码农都做不了架构师?转自http:blog.csdn.netztp800201articledetails9240913感谢作者分享Objecti ... [详细]
  • 深入解析 Lifecycle 的实现原理
    本文将详细介绍 Android Jetpack 中 Lifecycle 组件的实现原理,帮助开发者更好地理解和使用 Lifecycle,避免常见的内存泄漏问题。 ... [详细]
  • 本文探讨了利用JavaScript实现集合的对称差集算法的方法。该算法旨在处理多个数组作为输入参数,同时保留每个数组中元素的原始顺序。算法不会移除单个数组内的重复元素,但会删除在不同数组之间出现的重复项。通过这种方式,能够有效地计算出多个数组的对称差集。 ... [详细]
author-avatar
狮子座YAO
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有