📄 tt.java
字号:
import java.net.*;
import java.io.*;
public class TT
{
public static void main(String args[])
{
String host=null;
int port = 6666;
ServerSocket server = null;
try
{
server = new ServerSocket(port);
Socket mySocket = null;
try
{
if(args.length > 0)
{
host = args[0];
}
if(args.length > 1)
{
try
{
port = Integer.parseInt(args[1]);
}
catch(NumberFormatException e)
{
System.out.println("Port input error");
System.exit(0);
}
}
Client cl = new Client(host,port);
cl.start();
while(true)
{
try
{
System.out.println("now listening...");
mySocket = server.accept();
System.out.println(mySocket.getInetAddress().toString());
In in = new In(mySocket);
in.start();
Out out = new Out(mySocket);
out.start();
}
catch(Exception e){}
}
}
catch(Exception e){}
}
catch(Exception e){}
}
}
class Client extends Thread
{
private String host = null;
private int port = -1;
private Socket mySocket;
Client(String host,int port)
{
this.host = host;
this.port = port;
}
public void run()
{
try{
while(true)
{
mySocket = new Socket(host,port);
if(mySocket != null)
break;
}
In in = new In(mySocket);
in.start();
Out out = new Out(mySocket);
out.start();
}
catch(Exception e){}
}
}
class In extends Thread
{
private Socket mySocket;
In(Socket mySocket)
{
this.mySocket = mySocket;
}
public void run()
{
BufferedReader in = null;
String line = null;
try
{
in = new BufferedReader(
new InputStreamReader(mySocket.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
if(line == null)
break;
}
}
catch(Exception e)
{
System.out.println("error!! disconnected...");
}
}
}
class Out extends Thread
{
private Socket mySocket;
Out(Socket mySocket)
{
this.mySocket = mySocket;
}
public void run()
{
try{
String line;
BufferedReader serverin = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(mySocket.getOutputStream());
while(true)
{
line = serverin.readLine();
if(line == null)
break;
out.println(line);
out.flush();
}
}
catch (Exception e)
{}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -