简体   繁体   English

使用Java从客户端发送到服务器以及从服务器发送到客户端

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

I develop code to send form client to server then the server has to send what it receives from client again to client. 我开发代码以将客户端从客户端发送到服务器,然后服务器必须再次将从客户端接收到的内容发送到客户端。 but in my code the server only receives the message but does not resend it again. 但在我的代码中,服务器仅接收到该消息,但不再重发。 I do not know what is the problem 不知道是什么问题

This is my client code 这是我的客户代码

    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

I tried to use thread to receive message but also does not work. 我试图使用线程来接收消息,但也无法正常工作。 I tried without thread it does not work too. 我试过没有线程它也不起作用。

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

and this is my server code 这是我的服务器代码

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

this is main method for server 这是服务器的主要方法

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



}//end main

I do not have alot of knowledge in java especially about the socket thanks for your help 我对Java没有很多了解,尤其是关于套接字的知识,感谢您的帮助

a simply search for a java echo server shows this 只需搜索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();
        }
    }
}

see http://introcs.cs.princeton.edu/java/84network/EchoServer.java.html 参见http://introcs.cs.princeton.edu/java/84network/EchoServer.java.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM