网络学习笔记(单线程)

本地实现:
服务器文件(java实现):

package socket;

import java.io.*;
import java.net.*;


public class Server {

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根

        String readline = null;
        String inTemp = null;
        //String outTemp = null;
        String turnLine = "\n";
        final String client = "Client:";
        final String server = "Server:";

        int port = 60000;

        //首先直接创建serversocket
        ServerSocket serverSocket = new ServerSocket(port);//调用服务器的accept()进行阻塞(程序会在这等待),当有申请连接时会打开阻塞并返回一个socket
        Socket socket = serverSocket.accept();

        //创建三个流,系统输入流BufferedReader systemIn,socket输入流BufferedReader socketIn,socket输出流PrintWriter socketOut;
        BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter socketOut = new PrintWriter(socket.getOutputStream());

        while(readline != "bye"){

            inTemp = socketIn.readLine();
            System.out.println(client + turnLine + inTemp);
            System.out.println(server);

            readline = systemIn.readLine();

            //socketOut.print(readline);//python客户端通信用这个
            socketOut.println(readline);//java客户端通信用这个
            socketOut.flush();    //赶快刷新使Client收到,也可以换成socketOut.println(readline, ture)

            //outTemp = readline;

            //System.out.println(server);

        }

        systemIn.close();
        socketIn.close();
        socketOut.close();
        socket.close();
        serverSocket.close();

    }

}

客户端文件:

package socket;

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根

        String readline = null;
        String inTemp = null;
        //String outTemp = null;
        String turnLine = "\n";
        final String client = "Client:";
        final String server = "Server:";

        int port = 60000;
        byte ipAddressTemp[] = {127, 0, 0, 1};
        InetAddress ipAddress = InetAddress.getByAddress(ipAddressTemp);

        //首先直接创建socket,端口号1~1023为系统保存,一般设在1023之外
        Socket socket = new Socket(ipAddress, port);

        //创建三个流,系统输入流BufferedReader systemIn,socket输入流BufferedReader socketIn,socket输出流PrintWriter socketOut;
        BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter socketOut = new PrintWriter(socket.getOutputStream());

        while (readline != "bye") {

            System.out.println(client);
            readline = systemIn.readLine();
            //System.out.println(readline);

            socketOut.println(readline);
            socketOut.flush();    //赶快刷新使Server收到,也可以换成socketOut.println(readline, ture)

            //outTemp = readline;
            inTemp = socketIn.readLine();

            //System.out.println(client + outTemp);
            System.out.println(server + turnLine + inTemp);

        }

        systemIn.close();
        socketIn.close();
        socketOut.close();
        socket.close();

    }
}

非本地:
服务器文件(java实现)

package socket;

import java.io.*;
import java.net.*;


public class Server {

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根

        String readline = null;
        String inTemp = null;
        //String outTemp = null;
        String turnLine = "\n";
        final String client = "Client:";
        final String server = "Server:";

        int port = 60000;

        //首先直接创建serversocket
        ServerSocket serverSocket = new ServerSocket(port);//调用服务器的accept()进行阻塞(程序会在这等待),当有申请连接时会打开阻塞并返回一个socket
        Socket socket = serverSocket.accept();

        //创建三个流,系统输入流BufferedReader systemIn,socket输入流BufferedReader socketIn,socket输出流PrintWriter socketOut;
        BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter socketOut = new PrintWriter(socket.getOutputStream());

        while(readline != "bye"){

            inTemp = socketIn.readLine();
            System.out.println(client + turnLine + inTemp);
            System.out.println(server);

            readline = systemIn.readLine();

            socketOut.print(readline);//python客户端通信用这个
            //socketOut.println(readline);//java客户端通信用这个
            socketOut.flush();    //赶快刷新使Client收到,也可以换成socketOut.println(readline, ture)

            //outTemp = readline;

            //System.out.println(server);

        }

        systemIn.close();
        socketIn.close();
        socketOut.close();
        socket.close();
        serverSocket.close();

    }

}

客户机(python实现)

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('192.168.43.244',60000))//这里为服务器的ip地址和端口,记住端口要可用!!!
while True:
    re_data = input() +'\n'
    client.send(re_data.encode("ASCII"))
    data = client.recv(1024)
    print(data.decode("ASCII"))

最后实现时要先在客户机输入!
实现的是单线程。

上一篇:python 的open函数


下一篇:【JavaScript】7-37 整数分解为若干项之和 (20 分)