📄 m.txt
字号:
//-----------网络 1--------------
import java.net.*;
import java.io.*;
public class a {
public static void main(String[] args) throws Exception {
URL web = new URL("http://www.tsinghua.edu.cn:100");
System.out.println(web.getProtocol());
System.out.println(web.getHost());
System.out.println(web.getPort());
System.out.println(web.getFile());
BufferedReader in = new BufferedReader(
new InputStreamReader(web.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
-----------------------2--------------------------
import java.io.*;
import java.net.*;
public class a {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java Reverse string_to_reverse");
System.exit(1);
}
String stringToReverse=args[0];
URL url = new URL("http://java.sun.com/cgi-bin/backwards");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter to_cgi = new PrintWriter(connection.getOutputStream());
to_cgi.println("string=" + stringToReverse);
to_cgi.close();
BufferedReader from_cgi = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = from_cgi.readLine()) != null)
System.out.println(inputLine);
from_cgi.close();
}
}
//----------------3-----------------------
import java.net.*;
class a{
public static void main(String[] args){
try{
InetAddress ip1=InetAddress.getLocalHost();
System.out.println(ip1.getHostAddress()); //166.111.3.193
System.out.println(ip1.getHostName()); //wxy0
System.out.println(ip1);
InetAddress ip2=InetAddress.getByName("ccf.tsinghua.edu.cn");
System.out.println(ip2.getHostAddress()); //166.111.4.118
System.out.println(ip2.getHostName()); //ccf.tsinghua.edu.cn
InetAddress ip3=InetAddress.getByName("www.tsinghua.edu.cn");
System.out.println(ip3.getHostAddress()); //166.111.4.100
InetAddress ip4=InetAddress.getByName("mail.tsinghua.edu.cn");
System.out.println(ip3.getHostAddress());
}catch(Exception e){}
}
}
//---------------------4--------------------
import java.io.”
import java.net.*
public class c {
pbulic static void main(String args[]) {
try {
Socket socket = new Socket(“127.0.0.1”,8888);
BufferedReader sin = new BufferedReader (
new InputStreamReader( socket.getInputStream()));
PrintWriter sout = new PrintWriter (
socket.getOutputStream() );
BufferedReader input = new BufferedReader (
new InputStreamReader( System.in) );
String ask =input.readln();
while (!ask.equals(“end”)) {
sout.println(ask);
sout.flush();
System.out.println(“server answer: ” +
sin.readline() );
ask =input.readline();
}
sin.close(); sout.close(); socket.close();
} catch (Exception e) { }
} //main
} //class c
import java.io.”
import java.net.*
public class s {
pbulic static void main(String args[]) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket socket = ss.accept(); //
BufferedReader cin = new BufferedReader (new
InputStreamReader( socket.getInputStream()));
PrintWriter cout = new PrintWriter (
socket.getOutputStream() );
BufferedReader input = new BufferedReader (
new InputStreamReader( System.in) )
String answer ,ask ;
while (!ask.equals(“bye”)) {
ask = cin.readline()
System.out.println(“client ask :” + ask );
answer = input.readline() ;
cout.println(answer);
cout.flush();
}
cin.close(); cout.close();
socket.close(); ss.close() ;
} catch (Exception e) { }
} //main
} //class s
//-----------------------5-------------------
//------------client --------------------
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
class c {
public static void main(String args[]){
new MyClient();
}
}
class MyClient extends Frame
{
Socket ClientSocket;
PrintWriter output;
BufferedReader input;
String s;
TextArea textArea1;
TextArea textArea2;
Button MyButton = new Button("Send Message");
public MyClient(){
setTitle("Client Window"); //建立并显示与Server通讯的信息窗口
setSize(300,250);
setLayout(new BorderLayout());
textArea1=new TextArea(7,30);
textArea2=new TextArea(3,30);
add("North",textArea1);
add("Center",textArea2);
add("South",MyButton);
addWindowListener(new wd());
MyButton.addActionListener(new ButtonClick());
textArea2.addFocusListener(new TextFocus());
setVisible(true);
textArea1.setEditable(false);
textArea2.requestFocus();
connectToServer(); //与Server端连接通讯
}
public void connectToServer() {
try {
ClientSocket=new Socket(InetAddress.getLocalHost(),8000);
//连向Server主机的8000端口
output=new PrintWriter(
new OutputStreamWriter(ClientSocket.getOutputStream()));
input=new BufferedReader(
new InputStreamReader(ClientSocket.getInputStream()));
s=input.readLine(); //从Server端读入数据
textArea1.append(s+"\n");
} catch(Exception e){}
}
class ButtonClick implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
output.println(textArea2.getText());
output.flush();
s=input.readLine();
while(s.equals("")){
Thread.sleep(100);
s=input.readLine() ;
}
textArea1.append(s+"\n");
} catch(Exception ex){}
}
}
class TextFocus extends FocusAdapter{
public void focusGained(FocusEvent e){
textArea2.setText("");
}
}
class wd extends WindowAdapter { // To close the application
public void windowClosing(WindowEvent e) {
try{
output.println("Bye");
output.flush();
input.close();
output.close();
ClientSocket.close();
} catch(Exception ex){}
System.exit(0);
}
}
}
//------------------server-----------
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class s {
public static void main(String args[]) {
new ServerService(8000,10);
}
}
class ServerService extends Frame {
ServerSocket m_sListener; //监听服务程序
TextArea ListenerMeg; //监听程序窗口
ServerService(int port,int cnum) {
try{
m_sListener= new ServerSocket(port,cnum); //建立监听服务
setTitle("Server Listener");
setSize(300,200);
ListenerMeg = new TextArea("Server is listening on port "
+ port + "\n",10,50);
add("Center",ListenerMeg);
addWindowListener(new wd());
setVisible(true);
while(true) {
Socket Connected = m_sListener.accept();//接受来自Client端的请求
InetAddress ClientAddress = Connected.getInetAddress();
ListenerMeg.append( "Client " + " connected" +
" from \n"+ ClientAddress.toString() + " .\n" );
ServiceThread sThread = new ServiceThread(this,Connected);
//为与此Client通讯建立新线程
new Thread(sThread).start() ; //启动新线程
}
}catch(IOException e){}
}
public void addMeg(String s){ //在监听器窗口中加入信息
ListenerMeg.append(s);
}
class wd extends WindowAdapter { // To close the application:
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
} //ServerSerive Class
class ServiceThread extends Frame implements Runnable {
ServerService fss; //当前线程的父窗口
Socket tSocket; //当前线程负责通讯的Socket
InetAddress cadd ; //client端ip
TextArea tMeg; //当前线程信息显示窗口的文本区域
public ServiceThread (ServerService ss, Socket s ) {
fss = ss ;
tSocket = s;
setSize(300,200);
tMeg = new TextArea(10,50);
add("Center",tMeg);
cadd = tSocket.getInetAddress();
setTitle(" For client:" + cadd.getHostName());
setVisible(true);
}
public void run(){ //与Client端通讯
try {
BufferedReader in = new BufferedReader (
new InputStreamReader(tSocket.getInputStream()));
PrintWriter out = new PrintWriter (tSocket.getOutputStream());
out.println("Hello! Welcome to get server information service.\r");
out.flush(); //向Client端输出信息
String s = in.readLine(); //从Client端读入信息
while (!s.equals("bye")) {
out.println("You've send the following message:" + s);
out.flush();
tMeg.append("Client is saying: \n"+ s + "\n");
s = in.readLine();
}
out.println("good bye ");
out.flush();
tSocket.close(); //若Client端写入“bye”则结束通讯
}catch(Exception e){}
fss.addMeg( "Client " + cadd.toString() + " closed." + "\n" );
dispose();//close Frame
}//run()
}
-----------------运行说明--------------
打开一个DOS窗口,启动服务器程序 s.class
打开一个DOS窗口,启动一个客户端程序 c.class
打开一个DOS窗口,再启动一个客户端程序 c.class
......
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -