java tcp 链接的简单代码(tcp sever and client)
2013-05-30 21:10
531 查看
原文地址http://www.java-samples.com/showtutorial.php?tutorialid=1167
The Socket class is in the java.net package, so be sure to say import java.net.*; at the beginning of your file. The following is a simple example that illustrates the different portions of a server/client pair. This example works using localhost, which corresponds
to the default local computer IP address of 127.0.0.1. This way, both the server and the client will be running on the same computer. Server.java and Client.java contain the server and client source code for this simple example.
Here is the server code (Server.java):
The key portions of this program are in the try{} block. The ServerSocket instantiation is what sets up the server to listen at the given port. The server is automatically set up at the computer on which it is run. The Socket instantiation on the next line
uses the accept() method of ServerSocket. This method waits until a client attempts to connect to the server, and it returns an instance of the Socket class. This Socket instance (skt) is now the "warp tunnel" through which we can communicate with the client.
skt.getOutputStream() returns the output stream through which the server can talk to the client, and skt.getInputStream() returns the input stream through with the server can hear the client. This example creates a PrintWriter instance using the output stream
for easier output and sends the data (stored in data) to the client (out.print(data);). Bingo! Easy as that! After everything is done, all the streams and sockets shuold be closed before the program is exited. Now, let's see the client code.
Here is the client code (Client.java):
Once again, the meat of the program is in the try{} block. A connection to the server is attempted through the instantiation of the Socket class. It attempts to contact the server at localhost through port 1234 - the same port where the server is listening.
Once the socket is at hand, it works exactly the same as the one obtained through the ServerSocket class in Server.java. This time, the input stream is obtained and a BufferedReader is instantiated using it. The data is read from this stream and displayed
to the screen. Simple yet again!
The Socket class is in the java.net package, so be sure to say import java.net.*; at the beginning of your file. The following is a simple example that illustrates the different portions of a server/client pair. This example works using localhost, which corresponds
to the default local computer IP address of 127.0.0.1. This way, both the server and the client will be running on the same computer. Server.java and Client.java contain the server and client source code for this simple example.
Here is the server code (Server.java):
import java.lang.*; import java.io.*; import java.net.*; class Server { public static void main(String args[]) { String data = "Toobie ornaught toobie"; try { ServerSocket srvr = new ServerSocket(1234); Socket skt = srvr.accept(); System.out.print("Server has connected!\n"); PrintWriter out = new PrintWriter(skt.getOutputStream(), true); System.out.print("Sending string: '" + data + "'\n"); out.print(data); out.close(); skt.close(); srvr.close(); } catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); } } }
The key portions of this program are in the try{} block. The ServerSocket instantiation is what sets up the server to listen at the given port. The server is automatically set up at the computer on which it is run. The Socket instantiation on the next line
uses the accept() method of ServerSocket. This method waits until a client attempts to connect to the server, and it returns an instance of the Socket class. This Socket instance (skt) is now the "warp tunnel" through which we can communicate with the client.
skt.getOutputStream() returns the output stream through which the server can talk to the client, and skt.getInputStream() returns the input stream through with the server can hear the client. This example creates a PrintWriter instance using the output stream
for easier output and sends the data (stored in data) to the client (out.print(data);). Bingo! Easy as that! After everything is done, all the streams and sockets shuold be closed before the program is exited. Now, let's see the client code.
Here is the client code (Client.java):
import java.lang.*; import java.io.*; import java.net.*; class Client { public static void main(String args[]) { try { Socket skt = new Socket("localhost", 1234); BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream())); System.out.print("Received string: '"); while (!in.ready()) {} System.out.println(in.readLine()); // Read one line and output it System.out.print("'\n"); in.close(); } catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); } } }
Once again, the meat of the program is in the try{} block. A connection to the server is attempted through the instantiation of the Socket class. It attempts to contact the server at localhost through port 1234 - the same port where the server is listening.
Once the socket is at hand, it works exactly the same as the one obtained through the ServerSocket class in Server.java. This time, the input stream is obtained and a BufferedReader is instantiated using it. The data is read from this stream and displayed
to the screen. Simple yet again!
相关文章推荐
- 简单的tcp与udp通信 java代码
- Simple TcpClient and TcpServer in Java
- JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
- android--Socket(Socket and Client,TCP and UDP)在真机上运行,暂时留着代码
- 来段代码提提神-Java实现SocketServer和client链接
- Java简单实现爬虫技术,抓取整个整个网站所有链接+图片+文件(思路+代码)
- 简单的tcp/ip 协议java代码
- 简单用tcp实现一个client和sever间通信
- java基于TCP的socket编程简单实现[代码实践过]
- [JAVA] Tcp客户端和服务器简单代码
- Java链接redis并简单测试代码
- java学习之利用TCP实现的简单聊天示例代码
- TCP Sever Client 面向连接的实现
- java中的TCP SERVER代码
- 安卓简单适配-JAVA代码进行适配
- JAVA 实现自己的数据库驱动简单代码示例
- [Java]命令行模拟TCP客户端与服务端的简单小程序遇到的问题(基础不牢!扎实和亲手实践比什么都重要!)
- Java一个简单的爬虫:爬去网页代码
- 第一次动手写java的简单代码,实现输出一个空心菱形。
- 探究简单递归Java代码实现