Commit e3bbf3f0 authored by txp's avatar txp

服务端掉线,客户端等待

parent d040cfc8
...@@ -23,7 +23,7 @@ public class Client { // ...@@ -23,7 +23,7 @@ public class Client { //
InputStream is = null; InputStream is = null;
DataInputStream dis = null; DataInputStream dis = null;
try { try {
s = new Socket(InetAddress.getByName("192.168.216.1"), 8888); s = new Socket(InetAddress.getByName("192.168.3.3"), 8888);
/* 1.键盘录入账号和密码 */ /* 1.键盘录入账号和密码 */
sc = new Scanner(System.in); sc = new Scanner(System.in);
System.out.println("账号:"); System.out.println("账号:");
......
...@@ -6,6 +6,7 @@ public class Client { ...@@ -6,6 +6,7 @@ public class Client {
System.out.println("发送第"+i+"次"); System.out.println("发送第"+i+"次");
MySocketConnection.getSocket().getOutputStream().write(("发送第"+i+"次").getBytes("gbk")); MySocketConnection.getSocket().getOutputStream().write(("发送第"+i+"次").getBytes("gbk"));
}catch(Exception e){ }catch(Exception e){
System.out.println("服务器已断开, 正在重连...");
MySocketConnection.socket = null; MySocketConnection.socket = null;
sendMsg(i); sendMsg(i);
} }
......
...@@ -10,7 +10,7 @@ public class Main { ...@@ -10,7 +10,7 @@ public class Main {
i ++ ; i ++ ;
c.sendMsg(i); c.sendMsg(i);
try { try {
Thread.sleep(10000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -13,7 +13,7 @@ public class MySocketConnection { ...@@ -13,7 +13,7 @@ public class MySocketConnection {
public static Socket getSocket(){ public static Socket getSocket(){
try { try {
if(socket == null){ if(socket == null){
return new Socket(InetAddress.getByName("192.168.216.1"),12345); return new Socket(InetAddress.getByName("192.168.3.3"),8888);
}else{ }else{
return socket; return socket;
} }
......
...@@ -9,7 +9,7 @@ import java.net.UnknownHostException; ...@@ -9,7 +9,7 @@ import java.net.UnknownHostException;
public class Connect { public class Connect {
private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>(); private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>();
private static final String HOST = "192.168.216.1"; private static final String HOST = "192.168.3.3";
private static final int PORT = 8888; private static final int PORT = 8888;
...@@ -48,15 +48,15 @@ private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket> ...@@ -48,15 +48,15 @@ private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>
private static class KeepThread implements Runnable { private static class KeepThread implements Runnable {
public void run() { public void run() {
try { try {
System.out.println("=====================开始发送心跳包=============="); System.out.println("========开始发送心跳包========");
int i = 0;
while (true) { while (true) {
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("发送心跳数据包"); System.out.println("跳了" + i++ + "次");
outStr.write("send heart beat data package !".getBytes()); outStr.write("send heart beat data package !".getBytes());
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -69,7 +69,7 @@ private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket> ...@@ -69,7 +69,7 @@ private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>
private static class RecvThread implements Runnable { private static class RecvThread implements Runnable {
public void run() { public void run() {
try { try {
System.out.println("==============开始接收数据==============="); System.out.println("========开始接收数据========");
while (true) { while (true) {
byte[] b = new byte[1024]; byte[] b = new byte[1024];
int r = inStr.read(b); int r = inStr.read(b);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment