ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

IO与网络编程

2021-03-28 13:33:10  阅读:109  来源: 互联网

标签:socket 编程 网络 len IOException IO close new null


InetAddress类

这个类代表的是一个ip地址

ip地址

 

 常用方法:

InetAddress byName = InetAddress.getByName("www.baidu.com");

InetAddress byName1 = InetAddress.getByName("127.0.0.1");
InetAddress byName2 = InetAddress.getByName("localhost");
InetAddress localHost = InetAddress.getLocalHost();
byName1.getHostName();
byName1.getHostAddress();
===============================================
端口号:用来标识计算机上运行的进程。

 

 


======================================================
网络协议:

 

 

实现通信的几个步骤:
客户端:
1,创建socket对象,指明服务器端的ip和端口号
2,获取一个输出流,用于输出数据
3,输出数据
4,资源关闭
服务器端:
1,创建服务器端的serversocket,指明自己的端口号
2,调用accept方法,表示接受来自客户端的socket
3,获取输入流
4,读取输入流中的数据
5,关闭资源
public class TCPTest {

public void client () {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
os = socket.getOutputStream();
os.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}


}

public void server () {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream bs = null;
try {
serverSocket = new ServerSocket(8899);
accept = serverSocket.accept();
is = accept.getInputStream();

bs = new ByteArrayOutputStream();
byte[] buff = new byte[5];
int len;
while ((len = is.read(buff)) != -1) {
bs.write(buff, 0, len);
}
String s = bs.toString();
System.out.println(s);
System.out.println(accept.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bs != null)
bs.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}
==============================================================
从客户端发送一个文件到服务端:
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("Inet.iml"));
byte[] buf=new byte[1024];
int len;
while ((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
fis.close();
os.close();
socket.close();

}
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(9090);
Socket accept = serverSocket.accept();
InputStream is= accept.getInputStream();
FileOutputStream fos = new FileOutputStream("d\\1234.iml");

byte[] buff = new byte[5];
int len;
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}
fos.close();
is.close();
accept.close();
serverSocket.close();

}
============================================================
客户端给服务端发文件,然后服务端给客户端发个反馈

public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("Inet.iml"));
byte[] buf=new byte[1024];
int len;
while ((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
socket.shutdownOutput();//关闭数据输出

InputStream is = socket.getInputStream();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] buff = new byte[5];
int len1;
while ((len1 = is.read(buff)) != -1) {
bs.write(buff, 0, len1);
}
String s = bs.toString();
System.out.println(s);

bs.close();
is.close();
fis.close();
os.close();
socket.close();

}
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(9090);
Socket socket = serverSocket.accept();
InputStream is= socket.getInputStream();
FileOutputStream fos = new FileOutputStream("d\\1234.iml");

byte[] buff = new byte[5];
int len;
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}

OutputStream os = socket.getOutputStream();
os.write("你好,数据收到".getBytes());

os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();

}
=========================================================
UTP发送端给接收端发信息:
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str="我是东1";
byte[]by=str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket dp = new DatagramPacket(by,0,by.length,inet,9090);

socket.send(dp);
socket.close();
}
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[]buf=new byte[20];
DatagramPacket dp = new DatagramPacket(buf,0,buf.length);
socket.receive(dp);

System.out.println(new String(dp.getData(),0,dp.getLength()));//getData()可以返回一个字节数组
socket.close();
}
====================================================
URL编程:

 

 


常用方法:

 

 从网上下载个资源:

public class URLTest {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is= null;
FileOutputStream fos = null;
try {
URL url = new URL("http://=.........................");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("wode.jpg");
byte[]buf=new byte[1024];
int len;
while ((len=is.read(buf))!=-1){
fos.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
if (fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {if (urlConnection!=null)
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}




}
}







标签:socket,编程,网络,len,IOException,IO,close,new,null
来源: https://www.cnblogs.com/jmfy/p/14586887.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有