File tree Expand file tree Collapse file tree 4 files changed +125
-1
lines changed
Expand file tree Collapse file tree 4 files changed +125
-1
lines changed Original file line number Diff line number Diff line change 1+ package mypkg ;
2+
3+ import java .io .*;
4+ import java .net .*;
5+
6+ public class ServerThread extends Thread {
7+
8+ private Socket socket = null ;
9+
10+ public ServerThread (Socket socket ) {
11+ this .socket = socket ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ InputStream is =null ;
17+ InputStreamReader isr =null ;
18+ BufferedReader br =null ;
19+ OutputStream os =null ;
20+ PrintWriter pw =null ;
21+ try {
22+ is = socket .getInputStream ();
23+ isr = new InputStreamReader (is );
24+ br = new BufferedReader (isr );
25+
26+ String info = null ;
27+
28+ while ((info =br .readLine ())!=null ){
29+ System .out .println ("svr,cli:" +info );
30+ }
31+ socket .shutdownInput ();
32+
33+ os = socket .getOutputStream ();
34+ pw = new PrintWriter (os );
35+ pw .write ("服务器欢迎你" );
36+
37+ pw .flush ();
38+ } catch (Exception e ) {
39+ // TODO: handle exception
40+ } finally {
41+ try {
42+ if (pw !=null )
43+ pw .close ();
44+ if (os !=null )
45+ os .close ();
46+ if (br !=null )
47+ br .close ();
48+ if (isr !=null )
49+ isr .close ();
50+ if (is !=null )
51+ is .close ();
52+ if (socket !=null )
53+ socket .close ();
54+ } catch (IOException e ) {
55+ e .printStackTrace ();
56+ }
57+ }
58+ }
59+
60+ }
Original file line number Diff line number Diff line change 1+ package mypkg ;
2+ import java .net .*;
3+ import java .io .*;
4+
5+ public class SocketClient {
6+
7+ public static void main (String [] args ) throws InterruptedException {
8+ try {
9+ Socket socket = new Socket ("localhost" ,8088 );
10+ OutputStream os = socket .getOutputStream ();
11+ PrintWriter pw = new PrintWriter (os );
12+ pw .write (" cli msg" );
13+ pw .flush ();
14+
15+ socket .shutdownOutput ();
16+
17+ InputStream is = socket .getInputStream ();
18+ BufferedReader br = new BufferedReader (new InputStreamReader (is ));
19+ String info = null ;
20+ while ((info = br .readLine ())!=null ){
21+ System .out .println ("cli rcv:" +info );
22+ }
23+
24+ br .close ();
25+ is .close ();
26+ os .close ();
27+ pw .close ();
28+ socket .close ();
29+ } catch (Exception e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package mypkg ;
2+ import java .net .*;
3+ import java .io .*;
4+
5+ public class SocketServer {
6+
7+ public static void main (String [] args ) {
8+ try {
9+ ServerSocket serverSocket = new ServerSocket (8088 );
10+ Socket socket = new Socket ();
11+
12+ while (true ){
13+ socket = serverSocket .accept ();
14+
15+ ServerThread thread = new ServerThread (socket );
16+ thread .start ();
17+
18+ InetAddress address =socket .getInetAddress ();
19+ System .out .println ("cli ip:" +address .getHostAddress ());
20+ }
21+ } catch (Exception e ) {
22+ // TODO: handle exception
23+ e .printStackTrace ();
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 33set JAVA_HOME = D:\Program Files\Java\jdk-12.0.2
44set PATH = %PATH% ;%JAVA_HOME% \bin;
55
6- set srcs = ForScanner.java ForScannerChild.java TxtParseUtils.java
6+ set srcs = ForScanner.java ForScannerChild.java TxtParseUtils.java ServerThread.java SocketClient.java SocketServer.java
77
88for %%i in (%srcs% ) do (
99 javac -encoding utf-8 -h . %%i
@@ -13,5 +13,10 @@ for %%i in (%srcs%) do (
1313 echo %%i succ.................................
1414 )
1515)
16+
17+ mkdir mypkg
1618move *.class mypkg
19+
20+ java mypkg.SocketServer
21+
1722:proc_err
You can’t perform that action at this time.
0 commit comments