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();
}
}
}
}
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 {
}
接收端:
package com.tanghaibin.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import org.junit.Test;
public class UDPDemo {
}
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 {
}
class client2 extends JFrame implements ActionListener{
}
服务器端:
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 {
}
class server2 extends JFrame implements ActionListener{
}