您的位置:首页 > 理论基础 > 计算机网络

黑马程序员--网络编程

2014-06-20 23:29 155 查看
------- android培训java培训、期待与您交流! ----------

创建UDP传输的发送端。

思路:

1,建立udp的socket服务。

2,将要发送的数据封装到数据包中。

3,通过udp的socket服务将数据包发送出去。

4,关闭socket服务。

1,udpsocket服务。使用DatagramSocket对象。

DatagramSocket ds = new DatagramSocket(8888);

2,将要发送的数据封装到数据包中。

String str = "udp传输演示:哥们来了!";

//使用DatagramPacket将数据封装到的该对象包中。

byte[] buf = str.getBytes();

DatagramPacket dp = 

new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.100"),10000);

//3,通过udp的socket服务将数据包发送出去。使用send方法。

ds.send(dp);

//4,关闭资源。

ds.close();

}

}

建立UDP接收端的思路。

 1,建立udp socket服务,因为是要接收数据,必须要明确一个端口号。

 2,创建数据包,用于存储接收到的数据。方便用数据包对象的方法解析这些数据.

 3,使用socket服务的receive方法将接收的数据存储到数据包中。

 4,通过数据包的方法解析数据包中的数据。

 5,关闭资源

1,建立udp socket服务。

DatagramSocket ds = new DatagramSocket(10000);

2,创建数据包。

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,buf.length);

3,使用接收方法将数据存储到数据包中。

ds.receive(dp);//阻塞式的。

4,通过数据包对象的方法,解析其中的数据,比如,地址,端口,数据内容。

String ip = dp.getAddress().getHostAddress();

int port = dp.getPort();

String text = new String(dp.getData(),0,dp.getLength());

System.out.println(ip+":"+port+":"+text);

5,关闭资源。

ds.close();

}

}

package cn.itcast.net.p3.chat;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class Send implements Runnable {

private DatagramSocket ds;

public Send(DatagramSocket ds){

this.ds = ds;

}

public void run() {

try {

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

String line = null;

while((line=bufr.readLine())!=null){

byte[] buf = line.getBytes();

DatagramPacket dp = 

new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10001);

ds.send(dp);284 

 

if("886".equals(line))

break;

}

ds.close();

} catch (Exception e) {

}

}

}

 

(接收端)

package cn.itcast.net.p3.chat;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

public class Rece implements Runnable {

private DatagramSocket ds;

public Rece(DatagramSocket ds) {

this.ds = ds;

}

@Override

public void run() {

try {

while (true) {

// 2,创建数据包。

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, buf.length);

// 3,使用接收方法将数据存储到数据包中。

ds.receive(dp);// 阻塞式的。

// 4,通过数据包对象的方法,解析其中的数据,比如,地址,端口,数据内容。

String ip = dp.getAddress().getHostAddress();

int port = dp.getPort();

String text = new String(dp.getData(), 0, dp.getLength());

System.out.println(ip + "::" + text);

if(text.equals("886")){

System.out.println(ip+"....退出聊天室");

}

}

} catch (Exception e) {

}

}

}

(开启发送和接收两个线程开始运行聊天)

packagecn.itcast.net.p3.chat;

importjava.io.IOException;

importjava.net.DatagramSocket;

importjava.net.SocketException;

publicclassChatDemo {

/**

* @paramargs

* @throwsIOException 

*/

publicstaticvoidmain(String[] args) throwsIOException {

DatagramSocket send = newDatagramSocket();

 

DatagramSocket rece = newDatagramSocket(10001);

newThread(newSend(send)).start();

newThread(newRece(rece)).start();

}

}

使用TCP建立客户端:

 Tcp传输,客户端建立的过程。

 1,创建tcp客户端socket服务。使用的是Socket对象。

 建议该对象一创建就明确目的地。要连接的主机。

 2,如果连接建立成功,说明数据传输通道已建立。

 该通道就是socket流,是底层建立好的。既然是流,说明这里既有输入,又有输出。

 想要输入或者输出流对象,可以找Socket来获取。

 可以通过getOutputStream(),和getInputStream()来获取两个字节流。

 3,使用输出流,将数据写出。

 4,关闭资源。

创建客户端socket服务。

Socket socket = new Socket("192.168.1.100",10002);

//获取socket流中的输出流。

OutputStream out =socket.getOutputStream();

//使用输出流将指定的数据写出去。

out.write("tcp演示:哥们又来了!".getBytes());

//关闭资源。

socket.close();

}

}

使用TCP建立交互方式:

(客户端)

package cn.itcast.net.p4.tcp;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

public class ClientDemo2 {

public static void main(String[] args) throws UnknownHostException, IOException {

Socket socket = new Socket("192.168.1.100",10002);

OutputStream out = socket.getOutputStream();

out.write("tcp演示:哥们来了!".getBytes());

//读取服务端返回的数据,使用socket读取流。

InputStream in = socket.getInputStream();

byte[] buf = new byte[1024];

int len = in.read(buf);

String  text = new String(buf,0,len);

 

System.out.println(text);

//关闭资源。

socket.close();

}

}

 

(服务端)

package cn.itcast.net.p4.tcp;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

public class ServerDemo2 {

//1创建服务端对象。

ServerSocket ss = new ServerSocket(10002);

//2,获取连接过来的客户端对象。

Socket s = ss.accept();

String ip = s.getInetAddress().getHostAddress();

//3,通过socket对象获取输入流,要读取客户端发来的数据

InputStream in = s.getInputStream();

byte[] buf = new byte[1024];

int len = in.read(buf);

String text = new String(buf,0,len);

System.out.println(ip+":"+text);

//使用客户端socket对象的输出流给客户端返回数据

OutputStream out = s.getOutputStream();

out.write("收到".getBytes());

s.close();

ss.close();

}

}

网络编程小结:

最常见的客户端:

浏览器:IE。

最常见的服务端:

服务器:Tomcat。

为了了解其原理:

自定义服务端,使用已有的客户端IE,了解一下客户端给服务端发了什么请求?

发送的请求是:

GET / HTTP/1.1  请求行请求方式/web/test.html请求的资源路径http协议版本。

请求消息头. 属性名:属性值

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, 

application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*

Accept: */*     

Accept-Language: zh-cn,zu;q=0.5

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;InfoPath.2)

Host: 192.168.1.100:9090

//Host: www.huyouni.com:9090

Connection: Keep-Alive

//空行

//请求体。

//服务端发回应答消息。

HTTP/1.1 200 OK   //应答行,http的协议版本应答状态码应答状态描述信息

应答消息属性信息。属性名:属性值

Server: Apache-Coyote/1.1

ETag: W/"199-1323480176984"

Last-Modified:Sat, 10 Dec2011 01:22:56 GMT

Content-Type: text/html

Content-Length: 199

Date: Fri, 11 May 2012 07:51:39 GMT

Connection: close

//空行

//应答体。

<html>303 

<head>

<title>这是我的网页</title>

</head>

<body>

<h1>欢迎光临</h1>

<font size='5' color="red">这是一个tomcat服务器中的资源。是一个html网页。</font>

</body>

</html>

网络结构,

1,C/S  client/server

特点:

该结构的软件,客户端和服务端都需要编写。

可发成本较高,维护较为麻烦。

好处:

客户端在本地可以分担一部分运算。

2,B/S  browser/server

特点:

该结构的软件,只开发服务器端,不开发客户端,因为客户端直接由浏览器取代。

开发成本相对低,维护更为简单。

缺点:所有运算都要在服务端完成。

------- android培训java培训、期待与您交流! ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: