网络通信

来源:互联网 发布:ubuntu 14.04与16.04 编辑:程序博客网 时间:2024/04/28 23:36
网络通信的第一个元素:IP地址。通过IP地址,唯一的定位互联网上一台 
 * 主机
 *InetAddress:位于java.net包下
 *1.InetAddress用来代表IP地址。一个InetAddress的对象就代表着一个IP
 *地址
 *2.如何创建InetAddress的对象:getName(String host)
 *3.getHostName()获取IP地址对应的域名。getHostAddress()获取IP地址
 *
 *
 *
 *
 */
/**
 * A类网络的IP地址范围为1.0.0.1-127.255.255.254; B类网络的IP地址范围为:128.1.0.1-191.255.255.254;
 * C类网络的IP地址范围为:192.0.1.1-223.255.255.254。
 * 
 * 
 */
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
// 创建一个InetAddress对象
InetAddress inet = InetAddress.getByName("www.beicai.com");
// inet = InetAddress.getByName("119.90.141.3");
System.out.println(inet);
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
System.out.println();
// 获取本机的IP
InetAddress inet1 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());

}
}
public class TestInetAddress {public static void main(String[] args) throws UnknownHostException {// 创建一个InetAddress对象InetAddress inet = InetAddress.getByName("www.beicai.com");// inet = InetAddress.getByName("119.90.141.3");System.out.println(inet);System.out.println(inet.getHostName());System.out.println(inet.getHostAddress());System.out.println();// 获取本机的IPInetAddress inet1 = InetAddress.getLocalHost();System.out.println(inet1);System.out.println(inet1.getHostName());System.out.println(inet1.getHostAddress());}}
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import org.junit.Test;//TCP编程例一:客户端给服务端发送信息。服务端输出信息到控制台上public class TestTCP {// 客户端@Testpublic void client() {Socket socket = null;OutputStream os = null;try {// 1.创建socket对象,通过构造器指明服务端的IP地址,及其// 接收的端口号socket = new Socket("127.0.0.1", 9090);// 2.getOutputStream()发送数据,方法返回OutputStream// 的对象os = socket.getOutputStream();// 3.具体的输出过程os.write("你瞅啥?".getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}// 服务端@Testpublic void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;try {// 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号ss = new ServerSocket(9090);// 2.调用accept()方法,返回一个Socket的对象s = ss.accept();// 3.调用Socket对象的getInputStream()获取一个从客户端发送// 过来的输入流is = s.getInputStream();// 4.对获取的输入流进行操作byte[] b = new byte[20];int len;while ((len = is.read(b)) != -1) {String str = new String(b, 0, len);System.out.println(str);}} catch (IOException e) {e.printStackTrace();} finally {// TODO: handle finally clause// 5.关闭相应的流,Socket,ServerSocket的对象if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
//客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送回执
//给客户端
public class TestTCP2 {


// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket("10.114.19.237", 8989);
os = socket.getOutputStream();
os.write("你瞅啥?".getBytes());
// shutdownOutput():显示地告诉服务端发送完毕
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}


// 服务端
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(8989);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
os = s.getOutputStream();
os.write("瞅你咋地!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// TODO: handle finally clause
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}

}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


import org.junit.Test;
import java.io.InputStream;
import java.io.OutputStream;


//TCP编程例三:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”
//给客户端
public class TestTCP3 {
// 客户端
@Test
public void client() {
// 1.创建Socket的对象
Socket socket = null;
// 2.从本地获取一个文件发送给服务端
OutputStream os = null;
FileInputStream fis = null;
// 3.接收来自于服务端的消息
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
os = socket.getOutputStream();
fis = new FileInputStream(new File("1.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
os.write(b, 0, len);
}
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1 = is.read(b1)) != -1) {
String str = new String(b1, 0, len1);
System.out.println(str);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {


if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 4.关闭相应的流和服务端的消息
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


// 服务端
@Test
public void server() {
// 1.创建一个ServerSocket的对象
ServerSocket ss = null;
// 2.调用accept()方法,返回一个Socket的对象
Socket s = null;
// 3.将从客户端发送来的文件保存到本地
InputStream is = null;
FileOutputStream fos = null;
// 4.发送回执反馈给客户端
OutputStream os = null;
try {
ss = new ServerSocket(9898);


s = ss.accept();


is = s.getInputStream();
fos = new FileOutputStream(new File("2.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
System.out.println("收到来自于" + s.getInetAddress().getHostAddress() + "的文件");


os = s.getOutputStream();
os.write("头像上传成功".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流和Socket、ServerSocket的对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 福林与娘亲怀孕全文下载 和娘亲的故事福林 福林与娘亲出去打工 福林和娘亲共赴云雨 玉玉米米地地日日刘寡 玉米地和娘的民儿 慢点 民儿和娘玉米地的故事 第2段 民儿和娘在玉米地热逼 玉米地民儿与娘故事全文下载 玉米地娘和儿怀子小说 娘亲祖母共夫全集 小说下载 奶奶王珍珠怀孕txt 红高粱九儿玉米地视频 娘亲怀了我的种 民儿和娘玉米地故事 快抽出去外婆会怀孕的 民儿玉米地妈全给你 民儿和娘玉米地的小说 第章母亲播种危险期怀孕 儿子今晚后妈随便你怎么弄说说 同学说要看我骑他妈我说可以 荒村野性全文阅读目录 小成和后妈全文免费阅读文章 小诚和后妈全文免费阅读 娘亲在玉米地河边 我和娘亲玉米 娘亲给儿生个娃大结局 在玉米地插娘亲短文 在玉米地插娘亲电影 王来地里与娘激战2 在玉米地插娘亲播放 孩子别谢了我是你妈 臭小子 别急妈咪又不是不给你 粉嫩娘亲全文阅读全文 你的好粗妈v好痛 乡村孽缘上了王雪琴101一0 乡村小神医之情缠玉米地 乡村孽缘长篇小说全文免费阅读 乡村孽缘王雪琴怀孕是第几章 桃花村傻牛李大根 拨出来你爸要来