网络编程
通信双方地址
IP
端口号
要有一定的规则(网络通信协议,有两套参考模型)
OSI参考模型
TCP/IP协议,事实上的国际标准
域名也可以直接写,例如www.baidu.com
package wangluo;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
InetAddress inet1;
InetAddress inet2;
try {
inet1 = InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
inet2 = InetAddress.getByName("www.baidu.com");
System.out.println(inet2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
本地回路地址:127.0.0.1,对应着Localhost
InetAddress inet3 = InetAddress.getLocalHost(); //或者getHostName()\getHostAddress
System.out.println(inet3);
例1如下
package wangluo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
//客户端发送信息给服务端,服务端显示在控制台上
public class TCPTest1 {
@Test
//客户端
public void cilent(){
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("192.168.246.1");
socket = new Socket(inet,8899);
os = socket.getOutputStream();
os.write(("你好,我叫王子璇").getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
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 socket = null;
InputStream is = null;
//不建议这样写,可能会有点乱码
// byte[] buffer = new byte[20];
// int len;
// while((len = is.read(buffer)) != -1){
// String str = new String(buffer,0,len);
// System.out.println(str);
//
// }
ByteArrayOutputStream baos = null;
try {
ss = new ServerSocket(8899);
socket = ss.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于" + socket.getInetAddress().getHostAddress() + "的数据");
} catch (Exception e) {
e.printStackTrace();
// 关闭资源
}finally{
if(baos != null){
try {
baos.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(socket != null){
try {
socket.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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
// 客户端将文件发给服务端,服务端保存到本地,并返回发送成功给客户端
// 这里涉及到的异常应该使用try-catch-finally
public class TCPTest2 {
@Test
public void cilent() throws IOException{
Socket socket = new Socket(InetAddress.getByName("192.168.246.1"),9090);
// 输出流
OutputStream os = socket.getOutputStream();
// 输入流
FileInputStream fis = new FileInputStream(new File("1.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
socket.shutdownInput();
// 接受来自于服务器的数据
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bufferr = new byte[20];
int len1;
while((len1 = is.read(bufferr)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
fis.close();
os.close();
socket.close();
}
@Test
public void server() {
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
ServerSocket ss = new ServerSocket(9090);
socket = ss.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("5.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
// 服务器端给客户端反馈,
OutputStream os = socket.getOutputStream();
os.write("你好美女,我收到照片了".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
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(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}