练习
一、基于TCP_IP的网络编程练习:
一个IP地址与一个一个端口号构成一个Socket(套接字),网络编程时Socket编程。
socket.shutdownOutput();//如果有交互的话,需要用shutdown关闭。方便服务端知道读完了,可以执行其他操作了。
接收即输入用InputStream,InputStream的对象用read方法,只要是有Input的都要用数组接收一下,还有read;发送即输出用OutputStream,OutputStream的对象用write。
如果输入(InputStream...)的话要用到字符数组或者字节数组。
2、客户端给服务端发送文本,服务端会把文本转换为大写再返回给客户端。
package exercise;
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 java.util.Scanner;
import org.junit.Test;
/*
* 客户端给服务端发送文本,服务端将文本转成大写后再返回给客户端。
* */
public class TestTCP {
@Test
public void client() {
//1、Socket对象:指明你要发送到服务端的IP的地址以及要接受的端口号。
Socket socket=null;
//2、获取一个输出流,用于往服务端发送内容。
OutputStream os=null;
//3、向服务端发送从键盘输入的文本
Scanner scanner=null;
InputStream is=null;
try {
//1、Socket对象:指明你要发送到服务端的IP的地址以及要接受的端口号。
socket = new Socket(InetAddress.getByName("192.168.2.102"),9090);
//2、获取一个输出流,用于往服务端发送内容。
os = socket.getOutputStream();
// //3-1、向服务器端发送写死的文本
// os.write("abc".getBytes()); //传递写死的文字
//3、向服务端发送数据
System.out.println("请输入多个字符:");
scanner = new Scanner(System.in);
String str=scanner.next();//获取到输入的字符串
os.write(str.getBytes());//发送输入的
socket.shutdownOutput();//如果有交互的话,需要用shutdown关闭。方便服务端知道读完了,可以执行其他操作了。
//接受来自于服务端更改后的数据。
is = socket.getInputStream();
byte[] b=new byte[10];
int len;
while((len=is.read(b))!=-1) {
String str1=new String(b,0,len);
System.out.print(str1); //如果用println会每10个字符换一行。
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(scanner!=null) {
scanner.close();
}
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() {
//1、ServerSocket只写端口号
ServerSocket ss=null;
//2、获取一个Socket
Socket s=null;
//3、接受来自客户端的信息
InputStream is=null;
//4、返回给客户端
OutputStream os=null;
try {
//1、ServerSocket只写端口号
ss = new ServerSocket(9090);
//2、获取一个Socket
s = ss.accept();
//3、通过Socket获取一个输入流,接受来自客户端的信息
is = s.getInputStream();
//接受来自客户端的信息
byte[] b=new byte[10]; //只要是输入的都要考虑用字节或字符接收一下
int len; //
String str=new String();//String str=null;
while((len=is.read(b))!=-1) { //一批一批的接收,分几次。
String str1=new String(b,0,len);
str+=str1; //一次一次的接收到长的字符串中,最后str中有了整个长的字符串
}
// //调用现成的方法转换成小写的。
// String strUpperCase=str.toLowerCase();
// //返回给客户端即输出给客户端
// os = s.getOutputStream();
// //更改后的大写形式写出去。
// os.write(strUpperCase.getBytes());
//实现转换为大写字母
String strUpperCase=str.toUpperCase();
os=s.getOutputStream();
os.write(strUpperCase.getBytes());
} 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(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();
}
}
}
}
}
二、URL编程(类URL):
URL类的方法介绍:
代码展示:
package exercise;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/*
* URL:统一资源定位符,一个URL的对象,对应着互联网上的一个资源。
* 我们可以通过URL的对象调用其相应的方法,将此资源读取("下载")。
* */
public class TestURL { //为了方便下面直接用了throws抛出异常。
public static void main(String[] args) throws Exception {
//1、创建一个URL的对象
URL url=new URL("http://192.168.2.102:8088/test/1.mp3");//File file=new File("相对路径或绝对路径");
System.out.println(url.getProtocol());//等等很多方法。
//如何将服务端的资源读取进来:openStream()
InputStream is=url.openStream();
byte[] b=new byte[20];
int len;
while((len=is.read(b))!=-1) {
String str=new String(b,0,len);
System.out.println(str);
}
is.close();
//如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
URLConnection urlConn=url.openConnection();
InputStream is1=urlConn.getInputStream();
FileOutputStream fos=new FileOutputStream(new File("a.mp3"));
byte[] b1=new byte[20];
int len1;
while((len1=is1.read(b1))!=-1) {
fos.write(b1,0,len1);
}
fos.close();
is1.close();
}
}