2012-09-24 72 views
0

你好,我是java socket编程的新手,我只是想看看有人能给我一些帮助。Java服务器和客户端套接字需要修复

我将张贴在客户端和服务器的代码,然后我会解释我的问题...

  reader = new BufferedReader(new InputStreamReader(socket.getInputStream)); 

     while(running) 
     { 
      String line = reader.readLine(); 

      if(line != null) 
      { 
       System.out.println(line); 
       stream = new PrintStream(socket.getOutputStream()); 
       stream.println("return: " + line); 
      } 

     } 

    }catch(IOException e) 
    { 
     System.out.println("Socket in use or not available: " + port); 
    } 
} 

public static void main() 
{ 
    run(); 
} 


//Client 

public static String ip; 
public static int port; 

public static Socket socket; 
public static PrintStream stream; 
public static BufferedReader reader; 

public static void main(String args[]) 
{ 

    try 
    { 
     socket = new socket(ip, port); 
     stream = new PrintStream(socket.getOutputStream()); 
     stream.println("test0"); 
     reader = new BufferedReader(new InputStreamReader(socket.getInputStream)); 
     String line = reader.readLine(); 

     if(line != null) 
     { 
      System.out.println(line); 
     } 

     stream.println("test1"); 
     line = reader.readLine(); 

     if(line != null) 
     { 
      System.out.println(line); 
     } 
    }catch(IOException e) 
    { 
      System.out.println("could not connect to server!"); 
    } 
} 

所以我的问题是,即使我摆脱循环,并尽量使其发送字符串两次它不会发送它。它只会做一次,除非我关闭并在客户端创建一个新的套接字。所以,如果有人能给我一个解释我做错的事情,那会很棒,并且非常感谢你。

+0

找到它,如果的readLine()的'结果'为空则必须退出循环并关闭插座。否则,你将永远炒到CPU。 – EJP

回答

0

请保持它的简单,

尝试使用InputStream, InputStreamReader, BufferedReader, OutputStream, PrintWriter.

客户端:

Socket s = new Socket(); 
s.connect(new InetSocketAddress("Server_IP",Port_no),TimeOut); 
// Let Timeout be 5000 

服务器端:

ServerSocket ss = new ServerSocket(Port_no); 
Socket incoming = ss.accept(); 

对于从套接字读取:

InputStream is = s.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is); 

BufferedReader br = new BufferedReader(isr); 
boolean isDone = false; 

String s = new String(); 

while(!isDone && ((s=br.readLine())!=null)){ 

    System.out.println(s); // Printing on Console 

} 

用于写入套接字:

OutputStream os = s.getOuptStream(); 
PrintWriter pw = new PrintWriter(os) 

pw.println("Hello"); 
+0

感谢您的回复,我会仔细研究这段代码,看看我能如何运作。谢谢! –

+0

@BrandonLengefeld你是受欢迎的.... –

+0

我已经尝试了迄今为止所提出的所有改变,但没有运气。这可能是我的错,因为对此不够清楚。但是,我的问题是,当我发送类似“test0”的东西时,它会将其发送到服务器并重新发送没有问题,但是如果我在哪里发送“test1”没有运气去往/来自/来自服务器。它不应该保持稳定的流量还是我做错了什么? –

0

确保你从你的服务器刷新输出:

stream.flush(); 
1

你为什么在循环中打开你的外流? stream = new PrintStream(socket.getOutputStream());

将此语句写入循环外并写入您的循环中的stream

0

非常感谢所有回答我的人,但我知道它一直都是如此。 我通读了一些Oracle套接字的东西,发现服务器应该是第一个发送消息的客户端,而不是发送和接收客户端......等等等等,所以我会在这里发布我的新代码在希望别人试图找出同样的事情可以轻松

//Client  
public static String ip; 
public static int port; 

public static Socket socket; 
public static PrintWriter print; 
public static BufferedReader reader; 

public Client(String ip, int port) 
{ 
    this.ip = ip; 
    this.port = port; 

      //initiate all of objects 
    try 
    { 
     socket = new Socket(); 
     socket.connect(new InetSocketAddress(ip, port), 5000); 
     print = new PrintWriter(socket.getOutputStream()); 
     reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      //start connection with server 
     String line = reader.readLine(); 
     System.out.println(line); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

    //quick method to send message 
public void sendMessage(String text) 
{ 
    print.println(text); 
    print.flush(); 
    try 
    { 
     String line = reader.readLine(); 
     System.out.println(line); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

public static void main(String args[]) 
{ 
    client.sendMessage("test"); 
    client.sendMessage("test2"); 
    client.sendMessage("test3") 
} 

    //Server 


public static int port = 9884; 
public static boolean running = true; 

public static ServerSocket serverSocket; 
public static Socket socket; 
public static PrintWriter writer; 
public static BufferedReader reader; 

public static void run() 
{ 
    try 
    { 
     serverSocket = new ServerSocket(port); 
     socket = serverSocket.accept(); 
     writer = new PrintWriter(socket.getOutputStream(), true); 
     writer.println("connection"); 

     while(running) 
     { 
      reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      String line = reader.readLine(); 

      if(line != null) 
      { 
       System.out.println(line); 
       writer.println(line); 
       writer.flush(); 
      } 
     } 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void main(String args[]) 
{ 
    run(); 
} 
相关问题