java.net socket问题

qq_26305837 2017-12-06 05:20:45
我编写了一个socket和一个serversocket的交互程序,可是两者之间无法交互,但是调试软件下却正常
求解答,谢谢!

//client
package BillyLiang.MavenProjects;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.Scanner;

import nu.xom.ParsingException;
import nu.xom.ValidityException;

/**
* Hello world!
*@author Billy Herrington
*/
public class App //extends Frame1
{
Scanner scanner = null;
private String ADDRESS;
private int PORT;
private Socket client = null;
private TimeOut timeOut = new TimeOut();
public App() {
// super();
do {
this.interacting();
}while(!connect());
new Communicating(client);
}

private class Communicating implements Runnable{
private Socket comSocket = null;
private InputStream input = null;
private OutputStream output = null;
private Scanner
reader = null,
outputer = null;
private Thread
recieve = null,
send = null;
private final String beginning = ">>";
public Communicating(Socket socket) {
try {
this.comSocket = socket;
this.input = this.comSocket.getInputStream();
this.output = this.comSocket.getOutputStream();
this.reader = new Scanner(this.input);
this.recieve = new Thread(this,"MainThread");
this.send = new Thread() {
public void run() {
outputer = new Scanner(System.in);
while(true) {
try {
timeOut.turn(timeOut.ON);
output.write(outputer.nextLine().getBytes());
timeOut.turn(timeOut.OFF);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
System.exit(1);
}
}
}
};
this.recieve.start();
this.send.start();
}catch(IOException e) {
System.out.println(e.getMessage());
System.out.println("An IOException has benn thrown, exit...");
System.exit(1);
}
}

public void run() {
// TODO Auto-generated method stub
System.out.println(this.beginning + "It's time to have a chat below >_<");
while(true) {
try{
System.out.println(beginning + this.reader.nextLine() + "hop");
}catch(NoSuchElementException e) {
System.out.println("Server closed by itself, system exits...");
System.exit(1);
}
}
}
}

private void interacting() {
this.scanner = new Scanner(System.in);
System.out.println("Please enter the server IP address below:");
timeOut.turn(TimeOut.ON);
this.ADDRESS = scanner.nextLine();
timeOut.turn(timeOut.OFF);
timeOut.turn(TimeOut.ON);
System.out.println("Please enter the server listening port below:");
while(true) {
if(scanner.hasNextInt()) {
this.PORT = scanner.nextInt();
timeOut.turn(timeOut.OFF);
break;
}else {
System.out.println("Please enter again.");
scanner.nextLine();
}
}
System.out.println("Connecting to the server: " + this.ADDRESS + ":" + String.valueOf(this.PORT) + "...");
}

private boolean connect() {
try {
this.client = new Socket(this.ADDRESS,this.PORT);
System.out.println("Successfully connected to the server!");
return true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("Having errors when connecting...");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Having errors when connecting...");
return false;
}
}
private class TimeOut{
public static final int ON = 1;
public final int OFF = 0;
public final Integer TIMEOUT = 50000;
private Thread timing = null;
@SuppressWarnings("deprecation")
public void turn(int on) {
synchronized(TIMEOUT) {
if(on == ON) {
timing = new Thread() {
private long time0;
public void run() {
this.time0 = new Date().getTime();
while(new Date().getTime() - this.time0 < TIMEOUT) {}
System.out.println("Time's out(" + TIMEOUT/1000 + "s), exit...");
System.exit(0);
}
};
timing.start();
}else if(on == OFF) {
timing.stop();;
}
}
}

}
public static void main(String[] args) {
new Thread("App(Client) thread") {
public void run() {
new App();
}
}.start();
// new Thread("App2(Server) thread") {
// public void run() {
// try {
// new App2(1122);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
// }
// }.start();
}
}

以上为客户端

package BillyLiang.MavenProjects;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
* this application is used to create a server in order to accept sockets
* @author Billy
*
*/

public class App2 implements Runnable{
ServerSocket ss = null;
public App2(int port) throws IOException {
// TODO Auto-generated constructor stub
System.out.println("Welcome to the terminal server written by Billy Liang!\nListening at the port " + port +"...");
this.ss = new ServerSocket(port);
new Thread(this,"main").start();
}

class Entrance implements Runnable{
private Socket socket = null;
private Scanner in = null;
public Entrance( Socket arg0, int arg1) {
this.socket = arg0;
new Thread(this,"Socket:"+socket.getInetAddress().toString() + ":" + socket.getPort()).start();

}
public void run(){Scanner in = null;
// TODO Auto-generated method stub
try {
InputStream is = this.socket.getInputStream();
in = new Scanner(is);
while(true) {
String sout = "[" + Thread.currentThread().getName() + "] " + in.nextLine();
System.out.println(sout);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (NoSuchElementException e) {
System.out.println("[" + Thread.currentThread().getName() + "] " + ">>Socket closed by itself");
}finally {
in.close();
}
}
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new App2(1122);
}

public void run() {
// TODO Auto-generated method stub
int i = 0;
while(true) {
try {
new Entrance(this.ss.accept(), ++i);
System.out.println(">>new socket accepted:"+ i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

以上为服务端
...全文
174 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_26305837 2017-12-08
  • 打赏
  • 举报
回复
还有就是我拿自己编的程序和已经做好的网络调试工具进行测试却没有问题,就是用自己编的两个进行交互就出现了上述的问题。

qq_26305837 2017-12-08
  • 打赏
  • 举报
回复
我先启动了服务器端

然后启动客户端并且输入了端口号

回车连接上了以后

我在客户端上发信息,但是服务端未反应


可是当我在客户端退出程序时,服务器端却一下子把我刚发的信息全打印了出来,并且显示socket closed


代码我已展出,系统环境时OS X10.13.1,jdk9.0.1,请各位大佬们帮助!非常感谢!
天之上有什么 2017-12-07
  • 打赏
  • 举报
回复
两个文件都运行下,不行的话截图控制台信息

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧