热门标签 | 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.

 

源码:

 

 

 


推荐阅读
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • RouterOS 5.16软路由安装图解教程
    本文介绍了如何安装RouterOS 5.16软路由系统,包括系统要求、安装步骤和登录方式。同时提供了详细的图解教程,方便读者进行操作。 ... [详细]
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
  • express工程中的json调用方法
    本文介绍了在express工程中如何调用json数据,包括建立app.js文件、创建数据接口以及获取全部数据和typeid为1的数据的方法。 ... [详细]
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社区 版权所有