JAVA socket套接字问题
在客户端程序中有如下代码:BufferedReadersin=newBufferedReader(newInputStreamReader(System.in));Pri...
在客户端程序中有如下代码:
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline = sin.readLine();
while(!readline.equals("bye")){
os.println(readline);
os.flush();
System.out.println("client " + readline);
System.out.println("server " + is.readLine());
readline = sin.readLine();
}
在服务器端中也有相应的WHILE循环,当客户端与服务器端建立连接并通信过程中,只能客户端输入一条信息后,等待服务器端回复信息到达后才能再次发送下一条信息,请问如何修改才能使客户端连续发送信息,而不用等待服务器端回信后才能再次输入 展开
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline = sin.readLine();
while(!readline.equals("bye")){
os.println(readline);
os.flush();
System.out.println("client " + readline);
System.out.println("server " + is.readLine());
readline = sin.readLine();
}
在服务器端中也有相应的WHILE循环,当客户端与服务器端建立连接并通信过程中,只能客户端输入一条信息后,等待服务器端回复信息到达后才能再次发送下一条信息,请问如何修改才能使客户端连续发送信息,而不用等待服务器端回信后才能再次输入 展开
展开全部
做成两个线程啊
追问
做的好像有点问题,总是抛出异常,你有代码没,我想看一下
追答
手头上没有..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
采取长连接就可以不停的发。
单例模式,不设定超时,不关闭socket。
保持活的状态
transferSocket.setKeepAlive(true);
单例
public Socket getInstance(){
if(transferSocket==null){
try {
transferSocket=new Socket(transferServerIp, Integer.valueOf(transferServerport));
transferSocket.setKeepAlive(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return transferSocket;
}
不停的用这个类发就可以了。
发送的
public String fepTransServer(String message,int num) throws Exception {
String returnMessage = "";
socketQueueLock.lock();
System.out.println("message="+message);
try {
transferSocket = getInstance();/*new Socket(transferServerIp, Integer.valueOf(transferServerport));
transferSocket.setSoTimeout(CONFIG.maxtimeout);*/
aaaInputSocketStream = transferSocket.getInputStream();
aaaOutputSocketStream = transferSocket.getOutputStream();
DataOutputStream outStream;
outStream = new DataOutputStream(aaaOutputSocketStream);
outStream.writeBytes(message);
outStream.flush();
byte[] receiveMsg = new byte[num];
DataInputStream inStream = new DataInputStream(new BufferedInputStream(aaaInputSocketStream));
int beat = inStream.available();
if(beat >= 0){
int result = inStream.read(receiveMsg,0,num);
if(result == -1){
returnMessage = "网络连接出错!";
}
if(receiveMsg[0] == '\n' || receiveMsg[0] == '\r'){
returnMessage = "命令执行失败!";
}else{
returnMessage = new String(receiveMsg);
}
}else{
returnMessage = "beat <=0,socke连接错误!";
}
} catch (Exception e) {
throw e;
}finally{
socketQueueLock.unlock();
//destroy();
}
return returnMessage;
}
以上是我们用的。
单例模式,不设定超时,不关闭socket。
保持活的状态
transferSocket.setKeepAlive(true);
单例
public Socket getInstance(){
if(transferSocket==null){
try {
transferSocket=new Socket(transferServerIp, Integer.valueOf(transferServerport));
transferSocket.setKeepAlive(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return transferSocket;
}
不停的用这个类发就可以了。
发送的
public String fepTransServer(String message,int num) throws Exception {
String returnMessage = "";
socketQueueLock.lock();
System.out.println("message="+message);
try {
transferSocket = getInstance();/*new Socket(transferServerIp, Integer.valueOf(transferServerport));
transferSocket.setSoTimeout(CONFIG.maxtimeout);*/
aaaInputSocketStream = transferSocket.getInputStream();
aaaOutputSocketStream = transferSocket.getOutputStream();
DataOutputStream outStream;
outStream = new DataOutputStream(aaaOutputSocketStream);
outStream.writeBytes(message);
outStream.flush();
byte[] receiveMsg = new byte[num];
DataInputStream inStream = new DataInputStream(new BufferedInputStream(aaaInputSocketStream));
int beat = inStream.available();
if(beat >= 0){
int result = inStream.read(receiveMsg,0,num);
if(result == -1){
returnMessage = "网络连接出错!";
}
if(receiveMsg[0] == '\n' || receiveMsg[0] == '\r'){
returnMessage = "命令执行失败!";
}else{
returnMessage = new String(receiveMsg);
}
}else{
returnMessage = "beat <=0,socke连接错误!";
}
} catch (Exception e) {
throw e;
}finally{
socketQueueLock.unlock();
//destroy();
}
return returnMessage;
}
以上是我们用的。
追问
看的不是太懂……能给出具体的代码吗
追答
你的代码发的太少,我没法改。
其实基本上差不多,你可以去搜索了解一下socket长连接,短连接。
你的是短连接改成长连接就行了。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询