找回密码
 立即注册
企业管理 2022-09-23 84 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
C/S程序:表明的客户/服务器程序,每次需求编写两套程序
-MSN、QQ:通常会有两套程序,一个是服务器端,别的一个是客户端
B/S程序:表明的浏览器/服务器,可以理解为动态WEB,论坛
本季方案
redking.blog.51cto/attachment/200903/5/27212_1236255657HPox.png
Sock:TCP通信
数据报:UDP通信
Socket程序需求的类:
1、一切的用户用于服务器来讲都是一个Socket客户端。
2、在服务器上使用ServerSocket类接收客户端的Socket
Socket通信模型
redking.blog.51cto/attachment/200903/5/27212_1236255662getr.png
Socket编程的四个基本过程
redking.blog.51cto/attachment/200903/5/27212_1236255664kZYR.png
编写一个服务器程序:ServerSocket
redking.blog.51cto/attachment/200903/5/27212_12362556724yft.png
只要是网络连接都恳求有一个端口
publicServerSocket(intport)
publicSocketaccept()throwsIOException
通过此方法等候客户端的socket进行访问。
ServerSocket01服务器端代码:
redking.blog.51cto/attachment/200903/5/27212_1236255681FmiW.png
此服务器端的功能十分的简单,即是接收客户端的恳求,以后在屏幕上输出哈~
redking.blog.51cto/attachment/200903/5/27212_1236255683DzcJ.png
Windows中有一个telnet命令,通过此命令就可以直接连接到服务器了。
redking.blog.51cto/attachment/200903/5/27212_1236255688dGxc.png
客户端收到redking.blog.51cto字符串,一起服务器端显示“客户端回答结束~~~”
redking.blog.51cto/attachment/200903/5/27212_12362556918za9.png
只要是契合网络的协议标准,一切的客户端都可以连接到此服务器端上。
但是通常状况下,很少说直接去使用telnet连接,往往会编写一个客户端。
Socket
publicSocket(StringHOST,intport)throwsUnknownHOSTException,IOException
指定一个主机的IP地址和一个端口
ClientSocket01客户端代码:
redking.blog.51cto/attachment/200903/5/27212_1236255697YBa2.png
验证下作用,和telnet相同哈~
redking.blog.51cto/attachment/200903/5/27212_1236255700tGRN.png
以上代码验证了,程序需求编写两套程序:
·一个是服务器端
·别的一个是客户端
如今发现一切的代码只是履行一次就完了,那么能不能说履行屡次呢?由我的用户可以自己发出中止的命令。
这样的行为我们可以做一个Socket经典——Echo程序
用户发啥内容,服务器就会回答啥内容:
EchoServer代码:
importjava.io.
;
importjava.
;
publicclassEchoServer{
//此处为了去管理try...catch直接抛出了异常
publicstaticvoidmain(Stringargs)throwsException{
ServerSocketserver=null;
//输出必定使用打印流
PrintStreamout=null;
//服务器必定也要接收输入
BufferedReaderbuf=null;
//1.实例化一个服务器的监听端
server=newServerSocket(9999);
//可以使用一种死循环的方法接收内容
Socketclient=null;
while(true){
//不断接收内容
client=server.accept();
//预备好向客户端输出内容
out=newPrintStream(client.getOutputStream());
//并且客户端要有输入给服务器端
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
//下面先给出一个完整的信息提示
out.println(您好!欢迎光临:redking.blog.51cto);
out.println(输入bye表明退出哈~);
//一个用户要发很多的信息
while(true){
//接收客户端发送而来的内容
Stringstr=buf.readLine();
if(str==null){
//假如str为空就表明退出
break;
}else{
//假如输入的是bye则表明系统退出
if(bye.equals(str)){
break;
}
//可以对用户发来的信息进行回答
out.println(ECHO:+str);
}
}
//进行收尾办公
out.close();
buf.close();
client.close();
//假如要关闭服务器时可以设置象征
//server.close();
}
}
}
看下作用:
redking.blog.51cto/attachment/200903/5/27212_12362557017PQk.png
如今我们用telnet连接上去看下作用
redking.blog.51cto/attachment/200903/5/27212_1236255704K06q.png
输入bye退出连接
redking.blog.51cto/attachment/200903/5/27212_1236255722ITF0.png
一起可以屡次连接
redking.blog.51cto/attachment/200903/5/27212_1236255726D6kt.png
典型的包括了输入和输出
EchoClient代码:
importjava.io.
;
importjava.
;
publicclassEchoClient{
publicstaticvoidmain(Stringargs)throwsException{
Socketclient=null;
BufferedReaderbuf=null;
PrintStreamout=null;
//1.连接服务器
client=newSocket(localHOST,9999);
//接收服务器端的输入信息
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
System.out.println(buf.readLine());
System.out.println(buf.readLine());
//以后预备从键盘接收数据
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
StringuserInput=null;
out=newPrintStr
[零基础学JAVA]JavaSE使用部分-32.Java网络编程.doc

(C/S program: the indicated client/server program, two sets of programs are written each time
-MSN, QQ: There are usually two sets of programs, one is the server side and the other is the client side
B/S program: indicated browser/server, can be understood as dynamic WEB, forum
Program this season
redking.blog.51cto/attachment/200903/5/27212_1236255657HPox.png
Sock: TCP communication
Datagram: UDP communication
Classes required by the Socket program:
1. All users are a Socket client for the server.
2. Use the ServerSocket class on the server to receive the client's Socket
Socket communication model
redking.blog.51cto/attachment/200903/5/27212_1236255662getr.png
Four basic processes of Socket programming
redking.blog.51cto/attachment/200903/5/27212_1236255664kZYR.png
Write a server program: ServerSocket
redking.blog.51cto/attachment/200903/5/27212_12362556724yft.png
As long as the network connection requests a port
publicServerSocket(intport)
publicSocketaccept() throwsIOException
This method waits for the client's socket to access.
ServerSocket01 server-side code:
redking.blog.51cto/attachment/200903/5/27212_1236255681FmiW.png
This server-side function is very simple, that is, to receive the client's request, and then output it on the screen~
redking.blog.51cto/attachment/200903/5/27212_1236255683DzcJ.png
There is a telnet command in Windows, through which you can directly connect to the server.
redking.blog.51cto/attachment/200903/5/27212_1236255688dGxc.png
The client receives the redking.blog.51cto string, and the server displays "End of client answer~~~"
redking.blog.51cto/attachment/200903/5/27212_12362556918za9.png
As long as it conforms to the protocol standard of the network, all clients can connect to this server.
But under normal circumstances, it is rarely said to use the telnet connection directly, and a client is often written.
Socket
publicSocket(StringHOST,intport)throwsUnknownHOSTException,IOException
Specify a host's IP address and a port
ClientSocket01 client code:
redking.blog.51cto/attachment/200903/5/27212_1236255697YBa2.png
Verify the effect, the same as telnet~
redking.blog.51cto/attachment/200903/5/27212_1236255700tGRN.png
The above code verifies that the program needs to write two sets of programs:
One is the server side
·The other one is the client
Now that all the code is found to be executed only once, can it be said that it is executed multiple times? My users can issue the order to stop by themselves.
Such behavior we can make a Socket classic - Echo program
What content the user sends, the server will answer what content:
EchoServer code:
importjava.io.
;
importjava.
;
publicclassEchoServer{
//In order to manage try...catch throws an exception directly
publicstaticvoidmain(Stringargs)throwsException{
ServerSocketserver=null;
//The output must use the print stream
PrintStreamout=null;
//The server must also receive input
BufferedReaderbuf=null;
//1. Instantiate the listener of a server
server=newServerSocket(9999);
//You can use an infinite loop method to receive content
Socketclient=null;
while(true){
// keep receiving content
client=server.accept();
//Ready to output content to the client
out=newPrintStream(client.getOutputStream());
//And the client has input to the server
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
//The following first gives a complete information prompt
out.println(Hello! Welcome: redking.blog.51cto);
out.println(enter bye to indicate exit~);
//A user has to send a lot of information
while(true){
//Receive the content sent by the client
Stringstr=buf.readLine();
if(str==null){
//If str is empty, it means exit
break;
}else{
//If the input is bye, it means that the system exits
if(bye.equals(str)){
break;
}
// can answer the information sent by the user
out.println(ECHO: str);
}
}
// do the finishing touches
out.close();
buf.close();
client.close();
//If you want to close the server, you can set the symbol
//server.close();
}
}
}
Take a look at the effect:
redking.blog.51cto/attachment/200903/5/27212_12362557017PQk.png
Now we use telnet to connect to see the effect
redking.blog.51cto/attachment/200903/5/27212_1236255704K06q.png
Enter bye to exit the connection
redking.blog.51cto/attachment/200903/5/27212_1236255722ITF0.png
Can be connected multiple times together
redking.blog.51cto/attachment/200903/5/27212_1236255726D6kt.png
Typically includes input and output
EchoClient code:
importjava.io.
;
importjava.
;
publicclassEchoClient{
publicstaticvoidmain(Stringargs)throwsException{
Socketclient=null;
BufferedReaderbuf=null;
PrintStreamout=null;
//1. Connect to the server
client=newSocket(localHOST,9999);
//Receive input from the server
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
System.out.println(buf.readLine());
System.out.println(buf.readLine());
// prepare to receive data from the keyboard later
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
StringuserInput=null;
out=newPrintStr
[Zero Basic Learning JAVA] JavaSE Use Part-32.Java Network Programming.doc)

[下载]10482481518.rar




上一篇:从零开始学Java编程
下一篇:mysql5.0驱动程序