局域网内主机之间Socket通信实现

只能传输文字且只能在局域网内实现通信


主机1:

package SocketTest;

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Server extends JFrame implements Runnable,ActionListener {
	JTextField outMessage = new JTextField(12);
	JTextArea inMessage = new JTextArea(12,20);
	JButton b = new JButton("发送数据");
	Server(){
		super("Server");
		setSize(320,200);
		setVisible(true);
		JPanel p = new JPanel();
		b.addActionListener(this);
		p.add(outMessage);
		p.add(b);
		Container con = getContentPane();
		con.add(new JScrollPane(inMessage), BorderLayout.CENTER);
		con.add(p, BorderLayout.NORTH);
		Thread thread = new Thread(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		validate();
		thread.start();
	}
	public void actionPerformed(ActionEvent event){
		 byte b[] = outMessage.getText().trim().getBytes();
		 try{
			 InetAddress address = InetAddress.getByName("192.168.1.110");
			 DatagramPacket data = new DatagramPacket(b,b.length,address,1234);
			 DatagramSocket mail = new DatagramSocket();
			 mail.send(data);
		 }
		 catch(Exception e){}
	}
	public void run(){
		DatagramPacket pack = null;
		DatagramSocket mail = null;
		byte b[] = new byte[8192];
		try{
			pack = new DatagramPacket(b,b.length);
			mail = new DatagramSocket(5678);
		}
		catch(Exception e){}
		while(true){
			try{
				mail.receive(pack);
				String message = new String(pack.getData(),0,pack.getLength());
				inMessage.append("收到的数据来自"+pack.getAddress());
				inMessage.append("\n收到的数据是"+message+"\n");
				inMessage.setCaretPosition(inMessage.getText().length());
			}
			catch(Exception e){}
		}
	}
	public static void main(String [] args){
		new Server();
	}
}

主机2:

package SocketTest;

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Client extends JFrame implements Runnable,ActionListener {
	JTextField outMessage = new JTextField(12);
	JTextArea inMessage = new JTextArea(12,20);
	JButton b = new JButton("发送数据");
	Client(){
		super("Client");
		setBounds(350,100,320,200);
		setVisible(true);
		JPanel p = new JPanel();
		b.addActionListener(this);
		p.add(outMessage);
		p.add(b);
		Container con = getContentPane();
		con.add(new JScrollPane(inMessage), BorderLayout.CENTER);
		con.add(p, BorderLayout.NORTH);
		Thread thread = new Thread(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		validate();
		thread.start();
	}
	public void actionPerformed(ActionEvent event){
		 byte b[] = outMessage.getText().trim().getBytes();
		 try{
			 InetAddress address = InetAddress.getByName("192.168.1.106");
			 DatagramPacket data = new DatagramPacket(b,b.length,address,5678);
			 DatagramSocket mail = new DatagramSocket();
			 mail.send(data);
		 }
		 catch(Exception e){}
	}
	public void run(){
		DatagramPacket pack = null;
		DatagramSocket mail = null;
		byte b[] = new byte[8192];
		try{
			pack = new DatagramPacket(b,b.length);
			mail = new DatagramSocket(1234);
		}
		catch(Exception e){}
		while(true){
			try{
				mail.receive(pack);
				String message = new String(pack.getData(),0,pack.getLength());
				inMessage.append("收到的数据来自"+pack.getAddress());
				inMessage.append("\n收到的数据是"+message+"\n");
				inMessage.setCaretPosition(inMessage.getText().length());
			}
			catch(Exception e){}
		}
	}
	public static void main(String [] args){
		new Client();
	}
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值