JAVA中tcp/ip和UDP的使用

客户端代码的编写:

import java.awt.Color;


public class Talk extends JFrame {


private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField txtIp;
private JTextField textField_4;


private static PrintWriter pw=null;
private static BufferedReader br=null;
private static Socket s;
static Scanner scanner=new Scanner(System.in);
private JButton btnNewButton_1;
private JButton btnUdpsend;

Integer port;
String IP;
DatagramSocket sendSocket;
private JButton btnNewButton_2;



/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Talk frame = new Talk();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
* @throws SocketException 
*/
public Talk() throws SocketException {
setTitle("\u5355\u5411\u804A\u5929\u7A97");

final DatagramSocket receiveSocket = new DatagramSocket(3457);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 391, 511);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(10, 10, 355, 270);
contentPane.add(panel);
panel.setLayout(null);

textField_1 = new JTextField();
textField_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

String string;
try {
string = br.readLine();
System.out.println("Client读到:"+string);
// textField_1.setText(textField_1.getText()+string);
textField_1.setText((textField_1.getText()+string)+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

});
textField_1.setBounds(10, 68, 335, 184);
panel.add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setBounds(10, 37, 206, 21);
panel.add(textField_2);
textField_2.setColumns(10);

textField_3 = new JTextField();
textField_3.setBounds(263, 37, 82, 21);
panel.add(textField_3);
textField_3.setColumns(10);

txtIp = new JTextField();
txtIp.setBackground(Color.LIGHT_GRAY);
txtIp.setHorizontalAlignment(SwingConstants.CENTER);
txtIp.setText("IP");
txtIp.setBounds(10, 10, 66, 21);
panel.add(txtIp);
txtIp.setColumns(10);

textField_4 = new JTextField();
textField_4.setBackground(Color.LIGHT_GRAY);
textField_4.setText("\u7AEF\u53E3");
textField_4.setBounds(263, 10, 66, 21);
panel.add(textField_4);
textField_4.setColumns(10);

JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.WHITE);
panel_1.setBounds(10, 324, 355, 139);
contentPane.add(panel_1);
panel_1.setLayout(null);

textField = new JTextField();
textField.setBounds(10, 10, 335, 90);
panel_1.add(textField);
textField.setColumns(10);

JButton btnNewButton = new JButton("send");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

//
//while(true){
//System.out.println("Client端请输入:");
//textField_1.setText("Client端请输入:");
String name = "王王东:";
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);

ArrayList al = new ArrayList();
al.add(name);
al.add(time);
al.add(textField.getText());

String str = al.toString();
// textField.setText(null);
pw.println(str);
pw.flush();

String string;
try {
string = br.readLine();
System.out.println("Client读到:"+string);

textField_1.setText("Client读到:"+string);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


// try {
// br.close();
// pw.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }

}
});
btnNewButton.setBounds(252, 110, 93, 23);
panel_1.add(btnNewButton);

btnUdpsend = new JButton("UDPsend");
btnUdpsend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

try {

String string = textField.getText();
String name = textField_2.getText();
String str = name +":"+ string;


byte[] databyte = new byte[1000];

databyte = str.getBytes();

System.out.println(databyte.length);

String str1 = new String(databyte,"UTF-8");

System.out.println(str1.length());

DatagramPacket sendPacket = new DatagramPacket(databyte,
str.length(), InetAddress.getByName("175.3.15.19"), 5051);

sendSocket.send(sendPacket);

System.out.println("发送数据:" + string);

//DatagramSocket receiveSocket = new DatagramSocket(4050);
byte buf[] = new byte[1000];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
receiveSocket.receive(receivePacket);
String s = new String(receivePacket.getData(), 0,
receivePacket.getLength());
textField_1.setText(s);

} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}


});
btnUdpsend.setBounds(139, 110, 93, 23);
panel_1.add(btnUdpsend);

btnNewButton_2 = new JButton("UDP\u8FDE\u63A5");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

try {
sendSocket = new DatagramSocket(3456);
//DatagramSocket receiveSocket = new DatagramSocket(4050);
} catch (SocketException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
btnNewButton_2.setBounds(10, 110, 93, 23);
panel_1.add(btnNewButton_2);

btnNewButton_1 = new JButton("\u63D0\u4EA4IP\u7AEF\u53E3");
btnNewButton_1.setBounds(20, 290, 109, 23);
contentPane.add(btnNewButton_1);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

try {
// IP = textField_2.getText();
// port = Integer.parseInt(textField_3.getText());

s = new Socket("175.3.15.2",6666);
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
br=new BufferedReader(new InputStreamReader(s.getInputStream()));


sendSocket = new DatagramSocket();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




System.out.println("Client端请输入:");
//String str1 = "Client端请输入:";
textField_1.setText("Client端请输入:");
}
});
}
}

JAVA中tcp/ip和UDP的使用