云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > (完整word版)Java知识总结完整版,推荐文档

(完整word版)Java知识总结完整版,推荐文档

  • 62 次阅读
  • 3 次下载
  • 2025/6/3 4:47:01

} }

class Consumer implements Runnable { LanZi lz = null;

Consumer(LanZi lz) {this.lz = lz;} public void run() {

for(int i=0;i<20;i++) { ManTou mt = lz.pop();

System.out.println(\消费了:\

try{Thread.sleep((int)(Math.random() * 1000));} catch (InterruptedException e){ e.printStackTrace();} } } }

wait与sleep

wait时别的线程可以访问锁定对象(调用wait方法的时候必须锁定该对象) sleep时别的线程也不可以访问锁定对象

第10章 网络编程

参考模型

OSI参考模型 TCP/IP参考模型

应用层 应用层 传输层 表示层 网络层 会话层 物理+数据链路传输层 层 网络层 数据链路层 物理层 IP(Internet Protocol)

TCP(transmission control protocol) UDP(user data protocol)

Socket(插座的意思)

两个Java应用程序可通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个Socket

Soclet通常用来实现client-server连接

java.net包中定义的两个类Socket和ServerSoclet,分别用来实现双向连接的client和server端

建立连接时所需要的寻址信息为远程基选集的IP地址和端口号(Port number) TCP端口UDP端口是分开的,每一个有65536个端口(建议使用1024以后的)

TCP Socket通信模型 Server Clicent ------------------------------------------------------------------------------ ServerSocket ss(port #) Socket s(host,port #) ss.accept() //等待连接 (Attempt to connect) --------------------------------------- Socket对象 OutputStream --------------------------------------- OutputStream InputStream --------------------------------------- InputStream socket.close() --------------------------------------- socket.close() import java.net.*; import java.io.*;

public class TCPServer {

public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); while(true) {

Socket s = ss.accept(); DataInputStream dis = DataInputStream(s.getInputStream());

System.out.println(dis.readUTF());//阻塞的 dis.close(); s.close();

//System.out.println(\ } } }

import java.net.*; import java.io.*;

public class TCPClient{

public static void main(String[] args) throws Exception { Socket s = new Socket(\ OutputStream os = s.getOutputStream();

DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(\

new

dos.flush(); dos.close(); s.close(); } }

import java.io.*; import java.net.*;

import java.applet.Applet; public class talkserver {

public static void main(String args[]) {

try {

ServerSocket server = null; try {

server = new ServerSocket(4700); }catch(Exception e) {

System.out.println(\ }

Socket socket = null; try {

socket = server.accept(); }catch(Exception e) {

System.out.println(\ }

String line; BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream()));

PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

System.out.println(\ line = sin.readLine();

while (!line.equals(\ {

os.println(line); os.flush();

System.out.println(\

System.out.println(\

line = sin.readLine(); }

is.close(); os.close(); socket.close(); server.close(); }catch(Exception e) {

System.out.println(\ } } }

import java.io.*; import java.net.*;

public class talkclient {

public static void main(String args[]) {

try {

Socket socket = new Socket(\ BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); String readline;

readline = sin.readLine();

while (!readline.equals(\ {

os.println(readline); os.flush();

System.out.println(\

System.out.println(\ readline = sin.readLine(); }

os.close(); is.close(); socket.close(); }catch(Exception e) {

System.out.println(\ }

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

} } class Consumer implements Runnable { LanZi lz = null; Consumer(LanZi lz) {this.lz = lz;} public void run() { for(int i=0;i<20;i++) { ManTou mt = lz.pop(); System.out.println(\消费了:\ try{Thread.sleep((int)(Math.random() * 1000));} catch (InterruptedException e){ e.printStackTrace();} } } } wait与sleep wait时别的线程可以访问锁定对象(调

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com