在java中从客户端发送到服务器,从服务器发送到客户端

时间:2014-03-13 02:41:07

标签: java sockets

我开发代码以将表单客户端发送到服务器,然后服务器必须再次将从客户端接收的内容发送到客户端。但在我的代码中,服务器只接收消息,但不会再次重新发送。我不知道是什么问题

这是我的客户代码

    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中没有很多关于套接字的知识 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

只需搜索java echo服务器即可显示此

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