62,615
社区成员
发帖
与我相关
我的任务
分享
//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();
}
}
}
}