我的客户端程序:
public class ClientSocket implements Runnable{
private Socket server;
private DataInputStream clientIn = null;
private DataOutputStream clientOut = null;
private SocketClientGL socketClientGL = null;
public ClientSocket(Socket socket){
server = socket;
}
public void run(){
readSocket();
}
private void readSocket(){
try {
clientIn = new DataInputStream(server.getInputStream());
clientOut = new DataOutputStream(server.getOutputStream());
boolean more = true;
int c ;
String request,response;
response="1";
sendResponse(response);
request = "";
//就是这里c=clientIn.read()时报错Socket closed,可我没有关闭socket啊?
while((c = clientIn.read())!= -1){
request +=(char)c;
System.out.println("客户端接收到的值:"+request);
}
if(request.equals("11")){
System.out.println("取得服务端开始传输文件响应");
socketClientGL = new SocketClientGL();
}
else if (request.equals("22")){
System.out.println("取得服务端停止传输文件响应");
socketClientGL.close();
}
else{
System.out.println("啥也不是");
//more= false;
}
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("客户端IO连接异常:"+e);
//e.printStackTrace();
}
}
private void sendResponse(String response){
System.out.println("客户端发送的请求为:"+response);
byte[] send = response.getBytes();
System.out.println("send 的长度为:"+send.length);
try {
//clientOut.write(send);
//clientOut.writeUTF(response);
for(int i =0;i<response.length();i++){
clientOut.write(response.charAt(i));
}
clientOut.flush();
clientOut.close();//***跟这个有关系吗?
} catch (IOException e) {
// TODO 自动生成 catch 块
try {
server.close();
} catch (IOException e1) {
// TODO 自动生成 catch 块
System.out.println("客户端Socket关闭异常:"+e1);
e1.printStackTrace();
}
System.out.println("客户端发送请求异常:"+e);
e.printStackTrace();
}
}
public static void main(String[] arg){
Socket socket;
try {
socket = new Socket(InetAddress.getByName("localhost"),8000);
//socket.setSoTimeout(50001);
ClientSocket clientSocket = new ClientSocket(socket);
clientSocket.run();
} catch (UnknownHostException e) {
System.out.println("主函数异常1:"+e);
} catch (IOException e) {
System.out.println("主函数异常2:"+e);
}
}
}
服务端程序还处于运行状态但是客户端已经停止了,这是怎么回事啊?急等着用,谢谢
public class ClientSocket implements Runnable{
private Socket server;
private DataInputStream clientIn = null;
private DataOutputStream clientOut = null;
private SocketClientGL socketClientGL = null;
public ClientSocket(Socket socket){
server = socket;
}
public void run(){
readSocket();
}
private void readSocket(){
try {
clientIn = new DataInputStream(server.getInputStream());
clientOut = new DataOutputStream(server.getOutputStream());
boolean more = true;
int c ;
String request,response;
response="1";
sendResponse(response);
request = "";
//就是这里c=clientIn.read()时报错Socket closed,可我没有关闭socket啊?
while((c = clientIn.read())!= -1){
request +=(char)c;
System.out.println("客户端接收到的值:"+request);
}
if(request.equals("11")){
System.out.println("取得服务端开始传输文件响应");
socketClientGL = new SocketClientGL();
}
else if (request.equals("22")){
System.out.println("取得服务端停止传输文件响应");
socketClientGL.close();
}
else{
System.out.println("啥也不是");
//more= false;
}
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("客户端IO连接异常:"+e);
//e.printStackTrace();
}
}
private void sendResponse(String response){
System.out.println("客户端发送的请求为:"+response);
byte[] send = response.getBytes();
System.out.println("send 的长度为:"+send.length);
try {
//clientOut.write(send);
//clientOut.writeUTF(response);
for(int i =0;i<response.length();i++){
clientOut.write(response.charAt(i));
}
clientOut.flush();
clientOut.close();//***跟这个有关系吗?
} catch (IOException e) {
// TODO 自动生成 catch 块
try {
server.close();
} catch (IOException e1) {
// TODO 自动生成 catch 块
System.out.println("客户端Socket关闭异常:"+e1);
e1.printStackTrace();
}
System.out.println("客户端发送请求异常:"+e);
e.printStackTrace();
}
}
public static void main(String[] arg){
Socket socket;
try {
socket = new Socket(InetAddress.getByName("localhost"),8000);
//socket.setSoTimeout(50001);
ClientSocket clientSocket = new ClientSocket(socket);
clientSocket.run();
} catch (UnknownHostException e) {
System.out.println("主函数异常1:"+e);
} catch (IOException e) {
System.out.println("主函数异常2:"+e);
}
}
}
服务端程序还处于运行状态但是客户端已经停止了,这是怎么回事啊?急等着用,谢谢
跟这个有关系的..
当通讯的流关闭了,socket也就断开了. 服务器那边也是一样的.所以要再你想关闭socket的时候再关闭流 你先把clientOut.close();去掉看看
public class ServerSockets implements Runnable{
private Socket client;
private InputStream in = null;
private OutputStream out = null;
private SocketServerGL datagramServer = null;
private ThreadPool thPool;
public ServerSockets(Socket socket){
client = socket;
//basertp = baseport;
// thPool = new ThreadPool(20); //创建线程连接池 gaolԭ��luoxp
//thPool.setTimeout(5000l);
}
public void run() {
//gaol delete "super.run();"
//super.run();
readSocket(); }
private void readSocket(){
try {
in = client.getInputStream();
out = client.getOutputStream();
//clientIP = client.getInetAddress();
boolean more = true;
String request;
int c;
while(more){
request = new String();
System.out.println("服务端正在运行");
//当没有新的数据读进的时候就一直在read()这里等着 55555怎么会这样?
while((c = in.read()) != -1){
request +=(char)c;
System.out.println("request ="+request);
}
System.out.println("服务端接收到 "+request+" 请求");
if(request.equals("1")){//客户端发送开始传输文件请求client->server,其格式为:1
more = parseInputStream(request);
datagramServer = new SocketServerGL();
more = true;
}
if(request.equals("2")){//客户端发送停止传输文件请求client->server,其格式为:2
more = parseInputStream(request);
if(datagramServer !=null){
datagramServer.close();
}
more = true;
}
more = false;
}
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("IO出现异常:"+e);
e.printStackTrace();
}
}
private boolean parseInputStream(String request){
boolean send = false;
System.out.println("执行第 "+request+" 个命令");
int command = Integer.parseInt(request);
switch(command){
case 1:{
send = sendRequest(command);break;
}
case 2:{
send = sendRequest(command); break;
}
default:{
System.out.println("没有符合的命令");
break;
}
}
return send;
}
private boolean sendRequest(int command){
String send;
boolean hasSent = false;
if(command ==1){
send = "11";//服务端向客户端发送传输文件相应server->client;其格式为“11”
sendToClient(send);
hasSent = true;
}
else if(command == 1){
send = "22";//服务端向客户端发送停止传输文件响应server->client;其格式为“22”
sendToClient(send);
hasSent = true;
}
return hasSent;
}
private void sendToClient(String send){
try{
for(int i=0; i<send.length(); i++){
out.write(send.charAt(i));
}
out.flush();
out.close();
}catch(IOException ex){
/*
try{
client.close();
}catch(IOException e){
System.out.println("没能成功的向客户端发送信息1:"+e) ;
}*/
System.out.println("没能成功的向客户端发送信息2:"+ex) ;
}
}
public static void main(String[] arg){
ServerSocket svrsoc = null;
try {
svrsoc=new ServerSocket(8000);
System.out.println("Server start....");
int count = 0;
Socket soc=svrsoc.accept();
ServerSockets serverSockets = new ServerSockets(soc);
serverSockets.run();
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("服务端主函数异常:"+e);
e.printStackTrace();
}
}}
try{
for(int i=0; i<send.length(); i++){
out.write(send.charAt(i));
}
out.flush();
out.close();
}catch(IOException ex){
/*
try{
client.close();
}catch(IOException e){
System.out.println("没能成功的向客户端发送信息1:"+e) ;
}*/
System.out.println("没能成功的向客户端发送信息2:"+ex) ;
}
}
这里面有好几个close 你关了那边似乎是读不到数据的现在没时间有时间仔细跟你看看/
你参考下这个帖子