簡體   English   中英

使用Java從客戶端發送到服務器以及從服務器發送到客戶端

[英]send from client to server and from server to client in java

我開發代碼以將客戶端從客戶端發送到服務器,然后服務器必須再次將從客戶端接收到的內容發送到客戶端。 但在我的代碼中,服務器僅接收到該消息,但不再重發。 不知道是什么問題

這是我的客戶代碼

    public class Client implements Runnable{

private static Socket s = null;  
//private static BufferedOutputStream fromUser = null;
private static  DataInputStream fromServer = null;
private static InputStreamReader input = new InputStreamReader(System.in);
private static InputStreamReader inputstreamreader = null;
private static BufferedReader bufferedreader = null;
private static DataOutputStream fromUser = null;
private static int chara = 0;
private static String line = null;
static int port = 0; 
static String host = null;

//connect to server
@Override
 public void run() {
        try {
 inputstreamreader = new InputStreamReader(s.getInputStream());
 bufferedreader = new BufferedReader(inputstreamreader);

    //charr = fromClient.read();
    while(true){
    if ((line = bufferedreader.readLine()) != null){
      System.out.println(line);}
      if(line.equals(-1)){
      break;
      }


 }//end while
 }catch(NullPointerException e) {
    // do something other
}
 catch (IOException e) {
    System.out.println(e);
 }

}//end the run


//constructor with two arguments
public Client(String host, int port){
try{
s = new Socket (host,port);
}
catch(Exception e){}
}
//send message to from Client to Server

public static void sendToServer(){
try{
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream()));
chara =input.read();
while(true){
if (chara == '~'){
break;}
fromUser.write(chara);
fromUser.flush();
chara =input.read();
 }//end while
 }
 catch (IOException e) {
    System.out.println(e);
 }
}//end send message

我試圖使用線程來接收消息,但也無法正常工作。 我試過沒有線程它也不起作用。

public static void main(String [] args){

host = args[0];
port = Integer.parseInt(args[1]);

System.out.println("Start connection  .....");
Client client = new Client(host,port); 
  Thread thread = new Thread(client);
    thread.start();
//connect(host,port);


client.sendToServer();

client.close();



}//end main

這是我的服務器代碼

public class Server extends Thread{

private static Socket s = null;  
private static ServerSocket ss = null; 
private static DataOutputStream fromUser = null;
private static  DataInputStream fromClient = null;
private static InputStreamReader input = new InputStreamReader(System.in);
private static InputStreamReader inputstreamreader = null;
private static BufferedReader bufferedreader = null;
static String line=null;
 static String sendC;
private static int chara = 0; 
static int port = 0; 

//connect to server
static void connect(int port){
try{
    ss = new ServerSocket (port);
    System.out.println("Listening on port "+port+"...");
    s = ss.accept();
System.out.println("Has been connected .....");
  }
     catch (IOException e) {
       System.out.println(e);
     }

}//end of the connection method
//send message to from Client to Server
public static void sendToClient(String text){
try{
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream()));
sendC =text;
fromUser.write(sendC.getBytes());
fromUser.flush();

 }
 catch (IOException e) {
    System.out.println(e);
 }
}//end send message

public static void receiveFromClient(){
try {
 inputstreamreader = new InputStreamReader(s.getInputStream());
 bufferedreader = new BufferedReader(inputstreamreader);

    //charr = fromClient.read();
    while(true){
    if ((line = bufferedreader.readLine()) != null){
      System.out.println(line);
      sendToClient(line);}
      if(line.equals(-1)){
      break;
      }
      }//end while
 }catch(NullPointerException e) {
    // do something other
}
 catch (IOException e) {
    System.out.println(e);
 }
}//end send message

這是服務器的主要方法

public static void main(String [] args){
port = Integer.parseInt(args[0]);
System.out.println("Start connection  .....");
connect(port);
receiveFromClient();
//sendToClient();
close();



}//end main

我對Java沒有很多了解,尤其是關於套接字的知識,感謝您的幫助

只需搜索Java回顯服務器即可顯示此信息

public class EchoServer {

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

        // create socket
        int port = 4444;
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("Started server on port " + port);

        // repeatedly wait for connections, and process
        while (true) {

            // a "blocking" call which waits until a connection is requested
            Socket clientSocket = serverSocket.accept();
            System.err.println("Accepted connection from client");

            // open up IO streams
            In  in  = new In (clientSocket);
            Out out = new Out(clientSocket);

            // waits for data and reads it in until connection dies
            // readLine() blocks until the server receives a new line from client
            String s;
            while ((s = in.readLine()) != null) {
                out.println(s);
            }

            // close IO streams, then socket
            System.err.println("Closing connection with client");
            out.close();
            in.close();
            clientSocket.close();
        }
    }
}

參見http://introcs.cs.princeton.edu/java/84network/EchoServer.java.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM