java基础知识总结(五)
2018-05-01 本文已影响0人
路小白同学
1.网络编程基础
一些基础概念:
ip地址:标识网络上的一台主机 ,逻辑地址 可变
mac地址:标识网络上的一台主机,物理地址 不可变
端口:标识主机中的一个进程 0--65535 1024以下的为预留端口
协议:通信双方之间的约定和标准
URL:统一资源定位符
协议名://主机名:端口号/相对路径
OSI七层模型:从下至上依次是 物理-->数据链路-->网络-->传输-->会话-->表示-->应用
下层为上层提供服务。
传输层:
tcp:传输控制协议,面向连接,可靠
udp:用户数据报协议,非面向连接,不可靠
tcp简单测试 客服端服务器请求数据:
客户端eg:
package test;
import java.io.*;
import java.net.*;
public class TCPClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//向服务器发起连接
Socket s = new Socket("192.168.1.101",9000);
InputStream is = s.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
for (int i = 0; i < 30; i++)
{
String string = in.readLine();
System.out.println(string);
}
in.close();
}
}
服务器端代码:
eg:
package test;
import java.io.*;
import java.net.*;
public class TCPServer
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
//服务器进程绑定9000端口
ServerSocket ss = new ServerSocket(9000);
while(true)
{
//阻塞方法,等待客户端的连接请求
Socket socket = ss.accept();
System.out.println(socket.getInetAddress());
TcpServerThread threads = new TcpServerThread(socket);
threads.start();
}
}
}
class TcpServerThread extends Thread
{
Socket socket;
public void run()
{
OutputStream os;
try {
os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os);
for (int i = 0; i < 30; i++)
{
out.println("Hello +"+i);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TcpServerThread(Socket socket)
{
this.socket = socket;
}
}
udp简单测试 客服端服务器请求数据:
客户端代码:
package test;
import java.io.IOException;
import java.net.*;
public class UDPClient {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DatagramSocket ds = new DatagramSocket();
//发一封信给服务器
String string= "i am here";
byte[] bs1 = string.getBytes();
DatagramPacket sendLetter = new DatagramPacket(bs1, bs1.length,InetAddress.getLocalHost(),7000);
ds.send(sendLetter);
//从服务器收30封信
for (int i = 0; i < 30; i++)
{
DatagramPacket receiveLetter = new DatagramPacket(new byte[100], 100);
ds.receive(receiveLetter);
byte[] bs = receiveLetter.getData();
int offset = receiveLetter.getOffset();
int length = receiveLetter.getLength();
String s = new String(bs,offset,length);
System.out.println(s);
}
ds.close();
}
}
服务器代码:
package test;
import java.io.*;
import java.net.*;
import java.security.interfaces.DSAKey;
public class UDPServer {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
DatagramSocket ds = new DatagramSocket(7000);
//从客户端收信
DatagramPacket receiveLetter = new DatagramPacket(new byte[100], 100);
ds.receive(receiveLetter);
InetAddress address = receiveLetter.getAddress();
int port = receiveLetter.getPort();
System.out.println("收到了情求");
//给客服端发送30封信
for (int i = 0; i < 30; i++)
{
//显示信的内容
String str = "hello"+i;
byte[] bs = str.getBytes();
DatagramPacket sendLetter = new DatagramPacket(bs, 0,bs.length,address,port);
ds.send(sendLetter);
Thread.sleep(1000);
}
ds.close();
}
}