java网络编程

Posted 学不明白的小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java网络编程相关的知识,希望对你有一定的参考价值。

一:基于tcp的网络通信`

在这里插入代码片

/*服务器*/
package com.yxy;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer 

    /*服务器端*/
    public static void main(String[] args) throws IOException 
        ServerSocket serverSocket=new ServerSocket(8888);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream=new DataInputStream(inputStream);
        String s = dataInputStream.readUTF();
        System.out.println("客户端说:"+s);

        OutputStream outputStream=socket.getOutputStream();
        DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
        String s1="你好我是服务器";
        dataOutputStream.writeUTF(s1);
        dataOutputStream.close();
        dataInputStream.close();
        socket.close();
        serverSocket.close();
    


/*客户端*/

package com.yxy;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.*;
import java.net.Socket;

public class TestClient 
    /*客户端*/
    public static void main(String[] args) throws IOException 
        Socket socket=new Socket("192.168.0.103",8888);
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
        dataOutputStream.writeUTF("你好,我是客户端");


        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream=new DataInputStream(inputStream);
        String s = dataInputStream.readUTF();
        System.out.println("服务器返回消息"+s);
        dataOutputStream.close();
        dataInputStream.close();
        socket.close();

    

上面代码存在一个很严重的问题,服务器只能监听一次,那怎么可以解决了,在这了我提供一种方法,利用多线程,并且在上面增加一个简单的登录功能。这里没有连接数据

/*User*/
package com.yxy;

import java.io.Serializable;

public class User implements Serializable 
    private String username;
    private String password;

    public User() 
    

    public User(String username, String password) 
        this.username = username;
        this.password = password;
    

    public String getUsername() 
        return username;
    

    public void setUsername(String username) 
        this.username = username;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    


/*服务器线程*/
package com.yxy;

import jdk.internal.util.xml.impl.Input;

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread
    Socket socket=null;
    InputStream inputStream=null;
    ObjectInputStream objectInputStream=null;
    DataOutputStream dataOutputStream=null;
    OutputStream outputStream=null;
    public ServerThread(Socket socket)
        this.socket = socket;
    
    @Override
    public void run() 
        try 
            inputStream = socket.getInputStream();
            User user = (User) objectInputStream.readObject();
            boolean flag = false;
            if(user.getUsername().equals("yzy")&&user.getPassword().equals("1130"))
                flag = true;
            

            outputStream=socket.getOutputStream();
            dataOutputStream=new DataOutputStream(outputStream);
            dataOutputStream.writeBoolean(flag);
         catch (IOException e) 
            e.printStackTrace();
         catch (ClassNotFoundException e) 
            e.printStackTrace();
        finally 
            try 
                if(dataOutputStream!=null)
                    dataOutputStream.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                if(outputStream!=null)
                    outputStream.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                if(objectInputStream!=null)
                    objectInputStream.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                if(inputStream!=null)
                    inputStream.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
        
    

/*服务器*/
package com.yxy;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer 

    public static void main(String[] args) 
        System.out.println("服务器启动了");
        //1.创建套接字: 指定服务器的端口号
        ServerSocket serverSocket= null;
        Socket socket = null;
        int count=0;
        try 
            serverSocket=new ServerSocket(8888);
            socket=serverSocket.accept();
            new ServerThread(socket).start();
            count++;
            /*输出客户端信息*/
            System.out.println("当前是第"+count+"个用户访问我们的服务器,对应的用户是:"+socket.getInetAddress());
         catch (IOException e) 
            e.printStackTrace();
        
    


/*客户端*/
package com.yxy;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TestClient 
    /*客户端*/
    public static void main(String[] args) throws IOException 
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入username:");
        String username = scanner.next();
        System.out.println("请输入password:");
        String password = scanner.next();
        Socket socket=new Socket("192.168.0.103",8888);
        OutputStream outputStream = socket.getOutputStream();
        User user =new User(username,password);
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(user);



        InputStream inputStream=socket.getInputStream();
        DataInputStream dataInputStream=new DataInputStream(inputStream);
        boolean b = dataInputStream.readBoolean();
        if (b)
            System.out.println("登陆成功");
        else
            System.out.println("登录失败");
        
        dataInputStream.close();
        inputStream.close();
        objectOutputStream.close();
        outputStream.close();
        socket.close();

    

明天分享udp的一些基础知识,如果有什么不懂可以随时私信

以上是关于java网络编程的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

JAVA之AOP

java代码在片段活动中不起作用

java 代码片段【JAVA】