我想先让客户端传一条字符串给服务端,然后服务端再返回一条数据给客户端,但是每次总是程序被阻塞了。卡着动不了下面是我的源码,请帮我看下。。
SocketInterf.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class SocketInterf {
Socket client=null;
DataOutputStream out=null;
BufferedReader in=null;
String response;
String url="127.0.0.1";
int port=8000;
public void connection()
{
try
{
client=new Socket(url,port);
}catch (IOException e) {
System.out.println(e);
}
}
public void disConnection(){
try {
out.close();
in.close();
client.close();
} catch (IOException e) {
System.out.println(e);
}
}
public void dataSend(String data){
try {
out=new DataOutputStream(client.getOutputStream());
out.writeBytes(data);
} catch (IOException e) {
System.out.println(e);
}
}
public String dataReceive(){
String s=null;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
s=in.readLine();
} catch (IOException e) {
System.out.println(e);
}
return s;
}}TestSocket.javapublic class TestSocket { /**
* @param args
*/
public static void main(String[] args) {String s="sdfsalfpweekfsefmsfkmsfksafks";
String ss;
SocketInterf socketInterf=new SocketInterf();
socketInterf.connection();
socketInterf.dataSend(s);
System.out.println("已发送。。");ss=socketInterf.dataReceive();
System.out.println("已接收。。");
System.out.println(ss);
//System.out.println("eeeeeeeeeeeeeeee");
try{
socketInterf.disConnection();
}catch(Exception e){
System.out.println(e);
}}
}SocketServer.java
import java.io.*;
import java.net.*;
public class SocketServer {
public static void main(String args[]){
try{
ServerSocket server=null;
try{
server=new ServerSocket(8000); }catch(Exception e){
System.out.println("cann't listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept(); }catch(Exception e){
System.out.println("Error:"+e);
}
String s;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream())); DataOutputStream out=new DataOutputStream(socket.getOutputStream()); s=is.readLine();
System.out.println("Client:"+is.readLine());
System.out.println("已接收");
System.out.println("Client:"+s); out.writeBytes("tututuututututut");
out.flush();
System.out.println("已发送");
is.close();
out.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println("Error"+e);
}
}}
SocketInterf.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class SocketInterf {
Socket client=null;
DataOutputStream out=null;
BufferedReader in=null;
String response;
String url="127.0.0.1";
int port=8000;
public void connection()
{
try
{
client=new Socket(url,port);
}catch (IOException e) {
System.out.println(e);
}
}
public void disConnection(){
try {
out.close();
in.close();
client.close();
} catch (IOException e) {
System.out.println(e);
}
}
public void dataSend(String data){
try {
out=new DataOutputStream(client.getOutputStream());
out.writeBytes(data);
} catch (IOException e) {
System.out.println(e);
}
}
public String dataReceive(){
String s=null;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
s=in.readLine();
} catch (IOException e) {
System.out.println(e);
}
return s;
}}TestSocket.javapublic class TestSocket { /**
* @param args
*/
public static void main(String[] args) {String s="sdfsalfpweekfsefmsfkmsfksafks";
String ss;
SocketInterf socketInterf=new SocketInterf();
socketInterf.connection();
socketInterf.dataSend(s);
System.out.println("已发送。。");ss=socketInterf.dataReceive();
System.out.println("已接收。。");
System.out.println(ss);
//System.out.println("eeeeeeeeeeeeeeee");
try{
socketInterf.disConnection();
}catch(Exception e){
System.out.println(e);
}}
}SocketServer.java
import java.io.*;
import java.net.*;
public class SocketServer {
public static void main(String args[]){
try{
ServerSocket server=null;
try{
server=new ServerSocket(8000); }catch(Exception e){
System.out.println("cann't listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept(); }catch(Exception e){
System.out.println("Error:"+e);
}
String s;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream())); DataOutputStream out=new DataOutputStream(socket.getOutputStream()); s=is.readLine();
System.out.println("Client:"+is.readLine());
System.out.println("已接收");
System.out.println("Client:"+s); out.writeBytes("tututuututututut");
out.flush();
System.out.println("已发送");
is.close();
out.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println("Error"+e);
}
}}
server=new ServerSocket(8000);
socket=server.accept();
}catch(Exception e){
System.out.println(e);
}
try,catch会导致执行效率下降,机器不好都有可能死机!
最好是抛出异常,让下面的程序接受,然后一起做异常处理!就不会出现卡死现象了!