IO与网络编程

InetAddress类

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

ip地址

IO与网络编程

 

 常用方法:

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();
===============================================
端口号:用来标识计算机上运行的进程。

IO与网络编程

 

 


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

IO与网络编程

 

 

实现通信的几个步骤:
客户端:
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编程:

IO与网络编程

 

 


常用方法:

IO与网络编程

 

 从网上下载个资源:

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();
}
}




}
}







上一篇:[转]java.io.IOException 断开的管道 解决方法 ClientAbortException: java.io.IOException: Broken pipe


下一篇:网小鱼Java的bug小集锦0012