TCP/UDP编程

UDP:面向无连接的
        就是发条消息过去,不会管对方有没有接收到,就会断开

TCP:面向连接的
        连接上后就一直监听;等待下一次请求


UDP案例:
发送端:
package com.tanghaibin.socket;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;

import org.junit.Test;


public class UDPDemo2 {

    @Test
    public void sendUdp() throws IOException{
       
        System.out.println("发送端:");
       
        BufferedInputStream in = new BufferedInputStream(System.in);
       
        byte[] buf  = new byte[1024];
       
        in.read(buf);
       
        DatagramSocket ds = new DatagramSocket();
       
        DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"),5855);
       
        ds.send(dp);
       
       
    }
}



接收端:

package com.tanghaibin.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import org.junit.Test;


public class UDPDemo {
    @Test
    public void recive() throws IOException{
       
        System.out.println("接收端:");
       
        DatagramSocket ds = new DatagramSocket(5855);
       
        byte[] buf = new byte[1024];
       
        DatagramPacket dp = new DatagramPacket(buf, buf.length);

        ds.receive(dp);
       
        System.out.println(new String(buf,0,buf.length));
       
        System.out.println("接收端完毕");
    }
}




TCP案例:
界面版
客户端:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
public class client {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new client2();
    }
}
class client2 extends JFrame implements ActionListener{
    JTextArea ja=null;
    JTextField jf=null;
    JButton jb=null;
    JScrollPane jsp=null;
    JPanel jp=null;
    PrintWriter pw=null;
    public client2(){
        ja=new JTextArea();
        jf=new JTextField(20);
        jb=new JButton("发送");
        jb.addActionListener(this);
        jsp=new JScrollPane(ja);
        jp=new JPanel();
        jp.add(jf);
        jp.add(jb);
        this.add(jsp,BorderLayout.CENTER);
        this.add(jp,BorderLayout.SOUTH);
        this.setSize(400,300);
        this.setTitle("客户端");
        this.setVisible(true);
        this.setLocation(500,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try{
            Socket s=new Socket("127.0.0.1",1026);
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw=new PrintWriter(s.getOutputStream(),true);
            while(true){
                String str=br.readLine();
                ja.append("服务器:"+"\r\n"+str+"\r\n");
            }
        }
        catch(Exception e){
           
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==jb){
            pw.println(jf.getText());
            ja.append("客户端:"+"\r\n"+jf.getText()+"\r\n");
            jf.setText("");
        }
    }
}


服务器端:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class server {
    public static void main(String[] arg){
        new server2();
    }
}
class server2 extends JFrame implements ActionListener{
    JTextArea ja=null;
    JTextField jf=null;
    JButton jb=null;
    JScrollPane jsp=null;
    JPanel jp=null;
    PrintWriter pw=null;
    public server2(){
        ja=new JTextArea();
        jf=new JTextField(20);
        jb=new JButton("发送");
        jb.addActionListener(this);
        jsp=new JScrollPane(ja);
        jp=new JPanel();
        jp.add(jf);
        jp.add(jb);
        this.add(jsp,BorderLayout.CENTER);
        this.add(jp,BorderLayout.SOUTH);
        this.setSize(400,300);
        this.setTitle("服务器端");
        this.setVisible(true);
        this.setLocation(0,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try{
            //监听端口
            ServerSocket ss=new ServerSocket(1026);
            //等待连接
            Socket s=ss.accept();
            //建立缓冲输入流
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            //建立输出流
            pw=new PrintWriter(s.getOutputStream(),true);
            while(true){
                String str2=br.readLine();
                ja.append("客户端:"+"\r\n"+str2+"\r\n");
            }
        }
        catch(Exception e){
           
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource().equals(jb)){
            try {
                pw.println(jf.getText());
                ja.append("服务器:"+"\r\n"+jf.getText()+"\r\n");
                jf.setText("");
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值