java socket网络编程实例代码(含服务端和客户端)

本代码为java socket网络编程实例代码,包括客户端和服务端,实现客户端发送消息,服务端接收并反馈消息。 server.java为服务端代码。 client.java为客户端代码。

服务端代码server.java:


package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * by whg
 * 2018-5-23
 * 
 * socket 服务端
 */
public class Server {

    public static void main(String[] args) {
        try {
        	//创建socket链接,端口6000
            ServerSocket server=new ServerSocket(6000);
            System.out.println("server is start,waiting for client.");
            
            //接收客户端消息
            Socket socket=server.accept();
            InputStream inputStream=socket.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
            String temp=null;
            while((temp=bufferedReader.readLine())!=null){
                System.out.println("recv:"+temp);
            }
            
            //反馈给客户端消息
            OutputStream outputStream=socket.getOutputStream();
            PrintWriter printWriter=new PrintWriter(outputStream);
            printWriter.print("I have accepted.");
            printWriter.flush();
            socket.shutdownOutput();
            
            //关闭资源
            printWriter.close();
            outputStream.close();
            bufferedReader.close();
            inputStream.close();
            socket.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

客户端代码Client.java:


package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * by whg
 * 2018-5-23
 * 
 * socket 客户端
 */
public class Client {

    public static void main(String[] args) {
        try {
            //创建socket
            Socket socket=new Socket("127.0.0.1",6000);
            
            //发送消息
            OutputStream outputStream=socket.getOutputStream();
            PrintWriter printWriter=new PrintWriter(outputStream);
            printWriter.print("hello, i am wang.");
            printWriter.flush();
            socket.shutdownOutput();
            
            //接收消息
            InputStream inputStream=socket.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
            String temp=null;
            while((temp=bufferedReader.readLine())!=null){
                System.out.println("server reply:"+temp);
            }
            
            //关闭资源
            bufferedReader.close();
            inputStream.close();
            printWriter.close();
            outputStream.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

 

你可能感兴趣的:(java,socket)