java socket编程实例
作者:admin 日期:2012-05-21
这篇文章将为你介绍java的socket编程(Java socket programming),服务器端等待客户端的链接。客户端发起连接之后,可以发送数据。在这个例子中,客户端发送一句"Hi my server"。要想终止本次连接,客户端发送"bye"给服务器,服务器将会回发一个“bye”,然后连接变可以终止了。服务器端继续等待下一个连接。这两个程序要在同一个机器上运行。如果你想要在两个不同的机器上运行,你也只需要修改地址“localhost”为服务器端程序所在机器的ip即可。
- //服务器端
- import java.io.*;
- import java.net.*;
- public class Provider{
- ServerSocket providerSocket;
- Socket connection = null;
- ObjectOutputStream out;
- ObjectInputStream in;
- String message;
- Provider(){}
- void run()
- {
- try{
- //1. creating a server socket
- providerSocket = new ServerSocket(2004, 10);
- //2. Wait for connection
- System.out.println("Waiting for connection");
- connection = providerSocket.accept();
- System.out.println("Connection received from " + connection.getInetAddress().getHostName());
- //3. get Input and Output streams
- out = new ObjectOutputStream(connection.getOutputStream());
- out.flush();
- in = new ObjectInputStream(connection.getInputStream());
- sendMessage("Connection successful");
- //4. The two parts communicate via the input and output streams
- do{
- try{
- message = (String)in.readObject();
- System.out.println("client>" + message);
- if (message.equals("bye"))
- sendMessage("bye");
- }
- catch(ClassNotFoundException classnot){
- System.err.println("Data received in unknown format");
- }
- }while(!message.equals("bye"));
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- finally{
- //4: Closing connection
- try{
- in.close();
- out.close();
- providerSocket.close();
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- }
- void sendMessage(String msg)
- {
- try{
- out.writeObject(msg);
- out.flush();
- System.out.println("server>" + msg);
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- public static void main(String args[])
- {
- Provider server = new Provider();
- while(true){
- server.run();
- }
- }
- }
- //客户端
- import java.io.*;
- import java.net.*;
- public class Requester{
- Socket requestSocket;
- ObjectOutputStream out;
- ObjectInputStream in;
- String message;
- Requester(){}
- void run()
- {
- try{
- //1. creating a socket to connect to the server
- requestSocket = new Socket("localhost", 2004);
- System.out.println("Connected to localhost in port 2004");
- //2. get Input and Output streams
- out = new ObjectOutputStream(requestSocket.getOutputStream());
- out.flush();
- in = new ObjectInputStream(requestSocket.getInputStream());
- //3: Communicating with the server
- do{
- try{
- message = (String)in.readObject();
- System.out.println("server>" + message);
- sendMessage("Hi my server");
- message = "bye";
- sendMessage(message);
- }
- catch(ClassNotFoundException classNot){
- System.err.println("data received in unknown format");
- }
- }while(!message.equals("bye"));
- }
- catch(UnknownHostException unknownHost){
- System.err.println("You are trying to connect to an unknown host!");
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- finally{
- //4: Closing connection
- try{
- in.close();
- out.close();
- requestSocket.close();
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- }
- void sendMessage(String msg)
- {
- try{
- out.writeObject(msg);
- out.flush();
- System.out.println("client>" + msg);
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- public static void main(String args[])
- {
- Requester client = new Requester();
- client.run();
- }
- }
评论: 0 | 查看次数: 10198