zoukankan      html  css  js  c++  java
  • Java Socket网络编程

    服务器端:

     1 import java.net.*;
     2 import java.io.*;
     3 public class SocketServer extends Thread {
     4     private ServerSocket serverSocket;
     5     public SocketServer(int port) throws IOException
     6     {
     7         serverSocket=new ServerSocket(port);
     8         //等待客户连接的时间不超过6秒
     9         serverSocket.setSoTimeout(100000);
    10     }
    11     public void run()
    12     {
    13         while(true)
    14         {
    15             try
    16             {
    17                 System.out.println("等待远程连接,端口号为:"+serverSocket.getLocalPort());
    18                 Socket server =serverSocket.accept();
    19                 System.out.println("远程主机地址"+server.getRemoteSocketAddress());
    20                 //从客户端读数据
    21                 DataInputStream in=new DataInputStream(server.getInputStream());
    22                 System.out.println(in.readUTF());
    23                 //往客户端发送数据
    24                 DataOutputStream out=new DataOutputStream(server.getOutputStream());
    25                 out.writeUTF("谢谢连接我:"+server.getLocalSocketAddress()+"
    Goodbye!");
    26                 server.close();
    27             }
    28             catch(SocketException s)
    29             {
    30                 System.out.println("Socket timed out");
    31                 break;
    32             }
    33             catch(IOException e)
    34             {
    35                 e.printStackTrace();
    36                 break;
    37             }
    38         }
    39     }
    40     public static void main(String[] args)
    41     {
    42         int port=6066;
    43         try
    44         {
    45             Thread t=new SocketServer(port);
    46             t.run();
    47         }
    48         catch(IOException e)
    49         {
    50             e.printStackTrace();
    51         }
    52     }
    53 }

    客户端:

     1 import java.net.*;
     2 import java.io.*;
     3 public class SocketClient {
     4     public static void main(String[] args)
     5     {
     6         //服务器地址
     7         String serverName="localhost";
     8         //服务器端口号
     9         int port=6066;
    10         try
    11         {
    12             System.out.println("连接到主机:"+serverName+",端口号:"+port);
    13             Socket client=new Socket(serverName,port);
    14             System.out.println("远程主机地址:"+client.getRemoteSocketAddress());
    15             //向服务器传送消息
    16             OutputStream outToServer=client.getOutputStream();
    17             DataOutputStream out=new DataOutputStream(outToServer);
    18             out.writeUTF("Hello from"+client.getLocalSocketAddress());
    19             
    20             //获取服务器返回的信息
    21             InputStream inFromServer=client.getInputStream(); 
    22             DataInputStream in=new DataInputStream(inFromServer);
    23             System.out.println("服务器响应:"+in.readUTF());
    24             client.close();
    25         }
    26         catch(IOException e)
    27         {
    28             e.printStackTrace();
    29         }
    30     }
    31 
    32 }
  • 相关阅读:
    codeforces_1075_C. The Tower is Going Home
    leetcode_Stone Game_dp_思维
    leetcode_Counting Bits_dp
    Divide and Conquer_1.最大连续子数组
    python_MachineLearning_感知机PLA
    IIS中启用gzip压缩(网站优化)
    asp.net运行机制图
    asp.net 的那点事(2、浏览器和一般处理程序)
    asp.net 的那点事(1、当用户在浏览器地址栏输入了网址后,发生了什么?)
    android环境搭配 运行android sdk manager时出现错误问题解决
  • 原文地址:https://www.cnblogs.com/mingyao123/p/7464884.html
Copyright © 2011-2022 走看看