日期:2014-05-20  浏览次数:20906 次

再请教java socket接收发送
下边的代码,如果我去掉红色部分,client端就能执行通过,只是不能所收到server的返回值,但是如果我加上红色部分,程序就卡死在那里了,请问,我如何才能既发送到服务器端,又能接收到服务器的返回信息呢?
void beginlogin()
  {  
  String send_info=Main.sessionno.toString().trim()+","+Main.sessionpwd.toString().trim();
   
  try{
  link_server_ip=java.net.InetAddress.getByName("www.abc.net").getHostAddress().toString();
  link_sa=new java.net.InetSocketAddress(link_server_ip,java.lang.Integer.parseInt(link_sports));

  link_server.connect(link_sa);
  link_sosw=new OutputStreamWriter(link_server.getOutputStream());
  link_bwout=new BufferedWriter(link_sosw);
  link_in=new BufferedReader(c_isr=new InputStreamReader(link_server.getInputStream()));
   
  link_bwout.write(Main.flag.toString().trim()+","+Main.sessionno.toString().trim()+","+Main.sessionpwd.toString().trim());
  link_bwout.flush();
   
  String readstr=null;
while(true)
{

if((readstr=link_in.readLine())==null)
{
break;
}
else
{
jLabel1.setText((readstr=link_in.readLine()));
}
}
[size=16px][/size][b][/b]  
  link_server.close();
  }
  catch(Exception e)
  {
   
  }
   
  }

------解决方案--------------------
Java code

server
package com.half.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerSocketImpl {
    public static void main(String[] args){
        try {
            ServerSocket serverS=new ServerSocket(1234);
            Socket socketC=null;
            while(true){
                socketC=serverS.accept();
                DealSocket cl=new DealSocket(socketC);
                new Thread(cl).start();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        
    }

}