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

為了賬號安全,請及時綁定郵箱和手機立即綁定

我想實現(xiàn)服務器和客戶端都可以收發(fā)信息,不知道哪里錯了

package?com.jwy.bb;

import?java.io.BufferedReader;
import?java.io.BufferedWriter;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.OutputStreamWriter;
import?java.net.Socket;
import?java.net.UnknownHostException;

public?class?SocketClient?{

	public?static?void?main(String[]?args)?{
		SocketClient?client=new?SocketClient();
		client.start();
	}
	public?void?start(){
		Socket?socket=null;
		BufferedWriter?writer=null;
		BufferedReader?reader=null;
		BufferedReader?inputReader=null;
		
		try?{
			socket=new?Socket("127.0.0.1",9898);
			writer=new?BufferedWriter(new?OutputStreamWriter(socket.getOutputStream()));
			reader=new?BufferedReader(new?InputStreamReader(socket.getInputStream()));
			inputReader=new?BufferedReader(new?InputStreamReader(System.in));
			String?inputContent;
			String?receiveMsg;
			while((inputContent=inputReader.readLine())!=null){
				writer.write(inputContent+"\n");
				writer.flush();
			}
			while((receiveMsg=reader.readLine())!=null){
				System.out.println(receiveMsg);
			}
		}??catch?(IOException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}
		finally{
			try?{
				inputReader.close();
				reader.close();
				writer.close();
				socket.close();
			}?catch?(IOException?e)?{
				//?TODO?Auto-generated?catch?block
				e.printStackTrace();
			}
		}
	}

}



package com.jwy.aa;


import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;


import com.sun.imageio.spi.OutputStreamImageOutputStreamSpi;


public class SocketServer2 {

public static void main(String[] args) {

SocketServer2 socketserver2=new SocketServer2();

socketserver2.start();

}

public void start(){

ServerSocket socketserver=null;

Socket socket=null;

BufferedReader reader=null;

BufferedWriter writer=null;

BufferedReader inputContent=null;

try {

socketserver=new ServerSocket(9898);

System.out.println("server...");

socket=socketserver.accept();

System.out.println("server"+socket.hashCode()+"connection");

inputContent=new BufferedReader(new InputStreamReader(System.in));

reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

// writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

String receiveMsg;

String inputcontent;

while((inputcontent=inputContent.readLine())!=null){

writer.write(inputcontent+"\n");

writer.flush();

}

while((receiveMsg=reader.readLine())!=null)

{

System.out.println(receiveMsg);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

try {

reader.close();

socket.close();

writer.close();

inputContent.close();

socketserver.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}


正在回答

1 回答

?while((inputContent=inputReader.readLine())!=null){

????????????????writer.write(inputContent+"\n");

????????????????writer.flush();

????????????}

?while((receiveMsg=reader.readLine())!=null){

????????????????System.out.println(receiveMsg);

}

這兩個while循環(huán)有問題,他們并不會起到循環(huán)的作用,如果讀取自己輸入數(shù)據(jù)null,就跳出循環(huán)1 , 如果在判斷讀取對方的數(shù)據(jù)位null,也就跳出循環(huán)2, 這樣代碼就執(zhí)行過去了。


而且readLine()是一個阻塞方法,如果循環(huán)阻塞了,對方怎么發(fā)也接收不到。


所以我自己的解決辦法是,啟動兩個線程,一個負責發(fā)送,一個負責接收,互不干擾。如果你還有問題我們可以在討論。


1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
見證Android消息推送時刻
  • 參與學習       48182    人
  • 解答問題       210    個

本課程將一步步教你如何搭建出一個自己的Android推送平臺

進入課程

我想實現(xiàn)服務器和客戶端都可以收發(fā)信息,不知道哪里錯了

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號