我写了一个,想要让Client每隔10秒向Server端发送数据,为什么Client端的while循环不起作用呢,只发送一次就停了??、???Client端public class MyClient {
Reader read = null;
public static void test() {
try{
Socket socket = new Socket("127.0.0.1",6868);
System.out.println("6868连接成功"); //得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
//向服务器发送数据
//得到网络输出流
while(true) {
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("abc");
//Thread.sleep(60000);
} }catch(Exception ex) {
System.out.println("链接服务器失败");
TerminalThread tt = new TerminalThread();
TestThread ttt = new TestThread();
tt.start();
ttt.start();
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
test();
}
}
class TerminalThread extends Thread {
public void run(){
new MyServer().test();
}
}
class TestThread extends Thread {
public void run(){
new MyClient().test();
}
}
Server端public class MyServer {
public static void test() {
//给服务器开放一个端口
try {
ServerSocket ss = new ServerSocket(6868);
System.out.println("等待连接...");
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接");
//得到输出流,服务器向客户端发送数据
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("hello,welcome to cs world!");
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
if(!str.equals("") && str.equals("Procedure whether normal")) {
}
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test();
}}
Reader read = null;
public static void test() {
try{
Socket socket = new Socket("127.0.0.1",6868);
System.out.println("6868连接成功"); //得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
//向服务器发送数据
//得到网络输出流
while(true) {
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("abc");
//Thread.sleep(60000);
} }catch(Exception ex) {
System.out.println("链接服务器失败");
TerminalThread tt = new TerminalThread();
TestThread ttt = new TestThread();
tt.start();
ttt.start();
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
test();
}
}
class TerminalThread extends Thread {
public void run(){
new MyServer().test();
}
}
class TestThread extends Thread {
public void run(){
new MyClient().test();
}
}
Server端public class MyServer {
public static void test() {
//给服务器开放一个端口
try {
ServerSocket ss = new ServerSocket(6868);
System.out.println("等待连接...");
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接");
//得到输出流,服务器向客户端发送数据
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("hello,welcome to cs world!");
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
if(!str.equals("") && str.equals("Procedure whether normal")) {
}
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test();
}}
将
TerminalThread tt = new TerminalThread();
TestThread ttt = new TestThread();
tt.start();
ttt.start();放到main()方法里,估计就可以了
如果链接失败Server端程序没起来,所以放到那是如果链接失败运行server文件