简体   繁体   中英

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

a simply search for a java echo server shows this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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