第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Java TCP 客戶端/服務(wù)器套接字

Java TCP 客戶端/服務(wù)器套接字

慕碼人2483693 2022-12-21 10:52:04
我正在研究一個(gè)將使用套接字創(chuàng)建 TCP 服務(wù)器和客戶端的問題。對(duì)于客戶端代碼,我的目標(biāo)是反復(fù)提示用戶輸入句子S,將句子S發(fā)送到服務(wù)器,接收服務(wù)器的響應(yīng),并顯示接收到的消息和以毫秒表示的往返時(shí)間。在服務(wù)器上,我的目標(biāo)是創(chuàng)建一個(gè) TCP 服務(wù)器套接字,等待客戶端連接,接收消息,用客戶端的 IP 地址和端口號(hào)顯示它,將消息大寫,顯示消息,然后回顯“大寫”的消息。我正在嘗試使用 while (!(input.equals("done"){ ...do something },但是,無論我做什么都會(huì)陷入無限循環(huán)。我希望它是我忽略的簡單東西,但是我沒看到。TCPServer.javaimport java.net.*;import java.io.*;public class myFirstTCPServer {    public static void main(String[] args) throws IOException {        int servPort = 4999;        ServerSocket Sy = new ServerSocket(servPort);        Socket servSocket = Sy.accept();        InputStreamReader in = new InputStreamReader(servSocket.getInputStream());        BufferedReader bf = new BufferedReader(in);        String str = bf.readLine();        while (!(str.equals("done"))){            System.out.println("client connected");            InetAddress address = InetAddress.getLocalHost();            String ip = address.getHostAddress();            System.out.println("IP: " + ip);            System.out.println("Port: " + servPort);            System.out.println("Message from client: " + str.toUpperCase());            PrintWriter pr = new PrintWriter(servSocket.getOutputStream());            pr.println(str);            pr.flush();        }        servSocket.close();    }}TCPClient.javaimport java.net.*;import java.io.*;import java.util.Scanner;public class myFirstTCPClient {    public static void main(String[] args) throws IOException {        String S;        Scanner input = new Scanner(System.in);        System.out.println("Enter a sentence");        S = input.nextLine();        Socket clntSocket = new Socket(InetAddress.getLocalHost(), 4999);        while (!(S.equals("done"))){            double sent = System.nanoTime();            PrintWriter pr = new PrintWriter(clntSocket.getOutputStream());            pr.println(S);            pr.flush();                   }        clntSocket.close();    }}
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

您需要將閱讀器移入 while 循環(huán)。因?yàn)檫@是服務(wù)器等待讀取客戶端輸入的地方。


    public class myFirstTCPServer {

    public static void main(String[] args) throws IOException {


        int servPort = 4999;

        ServerSocket Sy = new ServerSocket(servPort);

        Socket servSocket = Sy.accept();

        System.out.println("client connected");


        InputStreamReader in = new InputStreamReader(servSocket.getInputStream());

        BufferedReader bf = new BufferedReader(in);


        String str ="";

        while (true)){


            str = bf.readLine();


            if(str.equals("done")) break;


            InetAddress address = servSocket.getInetAddress();

            String ip = address.getHostAddress();

            System.out.println("IP: " + ip);

            System.out.println("Port: " + servPort);


            System.out.println("Message from client: " + str);


            PrintWriter pr = new PrintWriter(servSocket.getOutputStream());

            pr.println(str.toUpperCase());

            pr.flush();

        }

        servSocket.close();

    }

    }  

然后更改客戶端:

public class myFirstTCPClient {

    public static void main(String[] args) throws IOException {

        String S="";

        Scanner input = new Scanner(System.in);


        // you need to provide your server ip/domain

        // InetAddress.getLocalHost() , still works but only works when 

        // your client is in the same machine. 

        Socket clntSocket = new Socket("127.0.0.1", 4999);


        while (!(S.equals("done"))){

            System.out.println("Enter a sentence");

            S = input.nextLine();

            double sent = System.nanoTime();

            PrintWriter pr = new PrintWriter(clntSocket.getOutputStream());

            pr.println(S);

            pr.flush();


            InputStreamReader in = new InputStreamReader(clntSocket.getInputStream());

            BufferedReader bf = new BufferedReader(in);


            String str = bf.readLine();

            System.out.println("Message from server: " + str);

            double received = System.nanoTime();

            double total = received - sent;

            System.out.println("Round Trip Time: " + (total/1000000.0));

        }

        clntSocket.close();

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-12-21
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)