2012-07-25 244 views
0

嗨,我想獲取有關套接字客戶端連接到同一臺機器中的多線程服務器進程的一些數據。服務器線程被正確觸發,並且客戶端IP被retreived確定,但我似乎無法通過連接發送字符串。 客戶Java套接字客戶端/服務器

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package solverapplet; 
import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.io.*; 

/** 
* 
* @author me 
*/ 
public class Solverapplet { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Socket s; 
     try { 
      s = new Socket("IP", 4445); 
      System.out.println(s.getPort()); 
      //DataInputStream in = new DataInputStream (s.getInputStream()); 
     BufferedWriter out = new BufferedWriter(
          new OutputStreamWriter(s.getOutputStream())); 
     out.write("gamma"); 
         out.newLine(); 
         out.flush(); 

     } catch (UnknownHostException ex) { 
      Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
} 

服務器線程

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package serversideserver; 
import java.io.*; 
import java.net.*; 
import java.security.*; 
import java.util.List; 
import java.sql.*; 
import com.google.gson.Gson; 


class doComms implements Runnable { 
    private Socket server; 

    private String line,input,ip; 

    doComms(Socket server, String ip) { 
     this.server=server; 
     this.ip=ip; 
    } 

    public void run() { 

     input=""; 

     try { 
     // Get input from the client 
     BufferedReader in = new BufferedReader(
          new InputStreamReader(server.getInputStream())); 

     PrintStream out = new PrintStream(server.getOutputStream()); 
     Connection conn = null; 
     try 
      { 
       String userName = "root"; 
       String password = ""; 
       String url = "jdbc:mysql://localhost/test"; 
       Class.forName ("com.mysql.jdbc.Driver").newInstance(); 
       conn = DriverManager.getConnection (url, userName, password); 
       System.out.println ("Database connection established"); 
       // create the java statement 
     Statement st = conn.createStatement(); 

     // ResultSet rs; 
     while((line = in.readLine()) != null && !line.equals(".")) { 
      // Now do the magic. 
     //Data data = new Gson().fromJson(line, Data.class); 
     System.out.println("LINE: " + line); 
     input=line; 
     st.executeUpdate("UPDATE `solvers` SET `online`='1',`ip`='"+ server.getInetAddress().toString().substring(1) +"' WHERE `user`='"+ line +"'"); 

     // input= data.getcmd(); 
      out.println("{\"captcha\":1,\"text\":\"abc\",\"is_correct\":\"true\"}"); 
     } 
      } 
      catch (Exception e) 
      { 
       System.out.println (e.toString()); 
      } 

     // Now write to the client 

     System.out.println("UPDATE `solvers` SET `online`='1',`ip`='"+ server.getInetAddress() +"' WHERE `user`='"+ input +"'"); 
     //out.println("Overall message is:" + input); 

     server.close(); 
     } catch (IOException ioe) { 
     System.out.println("IOException on socket listen: " + ioe); 
     ioe.printStackTrace(); 
     } 
    } 
} 

線路通過發送是空的。 建立連接

package serversideserver; 

import java.io.*; 
import java.net.*; 
import java.security.*; 
import java.sql.*; 
/** 
* Title:  Sample Server 
* Description: This utility will accept input from a socket, posting back to the socket before closing the link. 
* It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net 
* Copyright: Copyright (c) 2002 
* Company:  Kieser.net 
* @author B. Kieser 
* @version 1.0 
*/ 

public class Serversideserver { 

    private static int port=4445,portsolver=4445, maxConnections=0; 
    // Listen for incoming connections and handle them 
    public static void main(String[] args) { 
    int i=0; 

    try{ 
     ServerSocket listener = new ServerSocket(port); 
     Socket server; 

     long counter=0; 
      int counter1=0; 
    int id=0; 
     String ip="uninit"; 
     while((i++ < maxConnections) || (maxConnections == 0)){ 

     server = listener.accept(); 
     counter++; 
     doComms conn_c= new doComms(server,ip); 
     Thread t = new Thread(conn_c); 
     t.start(); 
     //System.out.println("counter "+ (counter % id)); 
     } 
    } catch (IOException ioe) { 
     System.out.println("IOException on socket listen: " + ioe); 
     ioe.printStackTrace(); 
    } 

    } 

} 
+0

有一個mysql連接發生那裏,但它都連接正確。 – Evan 2012-07-25 01:19:47

+4

你們是否真的寫了像你這樣的代碼?體面和格式正確,它傷害了眼睛! – 2012-07-25 01:25:58

+1

你確定你的服務器調用正確嗎?通常,您使用ServerSocket來接受傳入連接。你如何初始化服務器的套接字? – jeff 2012-07-25 01:33:22

回答

1

我看起來你已經有了一個位的計時問題。以下是具有正確時間的代碼。請注意,我刪除了手邊無關的代碼。

客戶端:它看起來像在客戶端中,您正在寫入套接字並立即終止應用程序(導致連接關閉)。 doComms類寫回到客戶端,所以我添加了代碼來讀取響應。但是,如果您不期待迴應,您仍然需要閱讀一個字節。這將允許你確保你得到了EOF而不是一些數據,並且它會阻止當前的線程並保持連接的活躍。

package solverapplet; 

import java.io.IOException; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.io.*; 

/** 
* 
* @author you 
*/ 
public class Solverapplet { 

    /** 
    * @param args the command line arguments 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException { 
     Socket s = null; 
     try { 
      // make connection 
      s = new Socket("localhost", 4445); 

      // define streams 
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
      BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 

      // write data 
      out.write("gamma"); 
      out.newLine(); 
      out.flush(); 

      // read response 
      String returnData = in.readLine(); 
      System.out.println(returnData); 

     } catch (UnknownHostException ex) { 
      Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(Solverapplet.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      // close connection 
      s.close(); 
     } 
    } 
} 

服務器:我已經改變了服務器,以允許在任何給定時間的最大連接數,而不是關閉已建立的連接的最大數目之後。另外請注意,我的線程不是守護進程。如果你想爲X數量的客戶端,然後關機,你需要一個機制,讓線程繼續關停的ServerSocket

package serversideserver; 

import java.io.*; 
import java.net.*; 

/** 
* Title:  Sample Server 
* Description: This utility will accept input from a socket, posting back to the socket before closing the link. 
* It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net 
* Copyright: Copyright (c) 2002 
* Company:  Kieser.net 
* @author B. Kieser 
* @version 1.0 
*/ 

public class Serversideserver { 

    private static int port    =4445; 
    private static int maxConnections =10; 

    private static int connections = 0; 

    synchronized static void connectionClosed() { 
     connections--; 
     Serversideserver.class.notify(); 
    } 

    /** 
    * The blocking mechanism to only allow <code>maxConnections<code> 
    * @throws InterruptedException 
    *  thrown if blocking thread is interupted 
    */ 
    private synchronized static void nextConnection() throws InterruptedException { 
     while(connections>=maxConnections) { 
      Serversideserver.class.wait(); 
     } 
    } 

    public static void main(String[] args) throws InterruptedException { 
     try{ 
      // server socket 
      ServerSocket listener = new ServerSocket(port); 
      // socket 
      Socket socket; 

      // Keep the server alive 
      while(true){ 
       // Blocks if we have reached the max 
       nextConnection(); 

       // Accept connection to client 
       socket = listener.accept(); 

       // define request service 
       doComms conn_c= new doComms(socket,socket.getInetAddress().getCanonicalHostName()); 
       Thread t = new Thread(conn_c); 
       t.setDaemon(false); 

       // run request service 
       t.start(); 
      } 
     } catch (IOException ioe) { 
      System.out.println("IOException on socket listen: " + ioe); 
      ioe.printStackTrace(); 
     } 
    } 
} 

doComms之前執行:沒有太大的這一類變化...我只是清理了一點點,並刪除不必要的代碼行。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package serversideserver; 
import java.io.*; 
import java.net.*; 


class doComms implements Runnable { 
    private Socket socket; 
    private String ip; 

    doComms(Socket socket, String ip) { 
     this.socket = socket; 
     this.ip = ip; 
    } 

    public void run() { 
     try { 
      // Define input/output 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      PrintStream out = new PrintStream(socket.getOutputStream()); 

      // Process requests until the EOF is found 
      String line; 
      while((line = in.readLine()) != null && !line.equals(".")) { 
       // print input 
       System.out.println("LINE: " + line); 

       // print process line 
       System.out.println("UPDATE `solvers` SET `online`='1',`ip`='"+ ip +"' WHERE `user`='"+ line +"'"); 

       // write response 
       out.println("{\"captcha\":1,\"text\":\"abc\",\"is_correct\":\"true\"}"); 
      } 

      socket.close(); 
     } catch (IOException ioe) { 
      System.out.println("IOException on socket listen: " + ioe); 
      ioe.printStackTrace(); 
     } finally { 
     Serversideserver.connectionClosed(); 
    } 
    } 
} 

希望這有助於:)

+0

超棒的傢伙。感謝時間是它似乎的問題:) – Evan 2012-07-25 09:12:56

0

你的問題是嚴重模糊,但如果你想知道爲什麼服務器無法回覆您的客戶端,這是因爲客戶端永遠不會從套接字讀取。

相關問題