网络应用编程
聊天程序概述:
服务器— --- 客户端(n个)
网络连接:
建立网络连接,使用类socket
Ip地址:4个0-255之间的数字
Socket:客户端套接字(ip地址+端口号)
ServerSocket:服务器套接字
例:
(Server)
Public class Server{
PrivateServerSocket serverSocket;
Public Server(){
Try{
//端口号为1056
serverSocket = newServerSocket(1056);
System.out.println(“serverstart…”);
//accept():监听并接受到此套接字的连接,监听是否有客户端连接服务器,返回socket类型
Socket client = serverSocket.accpet();
System.out.println(“havaclient connect…”);
}catch(IOException e){
e.printStackTrace();
}
}
Public static void main(String[] args){
New Server();
}
}
(client)
Public class Client{
PrivateSocket clientSocket;
Public Client(){
Try{
//服务器的ip地址,端口号
clientSocket = newSocket(“127.0.0.1”,1056);
}catch(Exceptione){
e.printStackTrace();
}
}
Public static void main(String[] args){
New Client();
}
}
信息的发送与接收:
服务器:
Public class Server{
PrivateServerSocket serverSocket;
Public Server(){
Try{
serverSocket = newServerSocket(1056);
System.out.println(“serverstart…”);
Socket client =serverSocket.accept();
//输出连接上服务器的客户端的ip地址
System.out.println(“客户的ip地址是:”+client.getLocalAddress().getHostAddress());
//2.接收客户端发过来的信息
InputStreamis = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Stringinfo = br.readLine();
System.out.println(“客户端发送消息:”+info);
//3.服务器向客户端发送信息
OutputStream os =client.getOuputStream();
PrintStream ps = newPrintStream(os);
Ps.println(“ hello client”);
}catch(Exception e){
e.printStackTrace();
}
}
Public static void main(String[] args){
New Server();
}
}
客户端:
Public class Client{
PrivateSocket clientSocket;
PublicClient(){
Try{
clientSocket = new Socket(“127.0.0.1”,1056);
//1.客户端向服务器发送信息:
OutputStream os = clientSocket.getOutputStream();
PrintStream ps = new PrintStream(os);
Ps.println(“hello server”);
//4.客户端接收服务器发送的信息
InputStream is =clientSocket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Stringinfo = br.readLine();
System.out.println(“服务器发送消息:”+info);
}catch(Exception e){
e.printStackTrace();
}
}
Public static void main(String[] args){
New Client();
}
}
线程与多线程:
线程的生命周期:新建(new),运行中(runnable),非运行状态(not runnable)(例:sleep),死亡(dead)
例:
Public class Test1 implements Runnable{
Thread th1,th2;
PublicTest1(){
Th1= new Thread(this);
Th2 = new Thread(this);
Th1.start();
Th2.start();
}
Publicstatic void main(String[] args){
NewTest1();
}
//覆盖接口方法(重写run方法)
//run方法中的方法体叫做线程体,线程要完成什么功能,执行什么步骤,都在run方法中//完成
Public void run(){
System.out.println(“线程运行了”);
Thread th= Thread.currentThread();
If(th ==th1){
System.out.println(“线程1运行了”);
}
If(th == th2){
System.out.println(“线程2运行了”);
}
}
}
线程同步:
例:
Public class Server implement Runnable{
PrivateServerSocket serverSocket;
PublicServer(){
Try{
serverSocket = newServerSocket(1056);
System.out.println(“serverstarted…”);
//创建监听线程,并启动
Thread thListener = newThread(this);
thListener.start();
}catch(IOException e){
e.printStackTrace();
}
}
Public static void main(String[] args){
New Server();
}
//始终监听客户端的请求
Public void run(){
While(true){
//accept要try,catch,此处省略
Socket client = serverSocket.accpet();
System.out.println(“有客户连接上服务器了,客户的ip地址是:”+client.getLocalAddress().getHostAddress());
//为每个客户端的通信创建子线程
subThread sth =new SubThread(client);
sth.start();
}
}
}
重新创建一个类(此类中所有try,catch全部省略)
Public class SubThread extends Thread{
PrivateSocket client;
PublicSubThread(Socket client){
//左侧this.client代表成员变量client,右边是参数变量client
This.client= client;
}
//实现跟客户端的信息交互
Public void run(){
InputStream is =client.getInputStream();
BufferedReader br = newBufferedReader(new InputStreamReader(is));
String info = br.readline();
System.out.println(“客户发送过来的消息是”+info);
System.out.println(“输入你想说的信息“);
BufferedReader br1 = newBUfferedReader(new InputStreamReader(System.in));
String info1 = br1.readline;
OutputStream os =client.getOutputStream();
PrintStream ps =client.getOutputStream(os);
Ps.println(info1);
}
}