当前位置: 编程技术>移动开发
本页文章导读:
▪Java-UDP通讯(双向) Java--UDP通信(双向)新建两个工程,一个Send,一个Receive,先启动Receive,再启动Send,至此就可以双向通信
Send工程:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
impo.........
▪ Java-Socket通讯(双向,有界面) Java--Socket通信(双向,有界面)
服务端:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import.........
▪ 您正在使用的移动电话已经 40 岁 你正在使用的移动电话已经 40 岁
嗨 Joel,我是 Marty。我正在用手机给你打电话,是一部真正的移动手持电话哦。
1973 年 4 月 3 日,摩托罗拉的工程师 Marty Cooper 首次使用手机打通电话。.........
[1]Java-UDP通讯(双向)
来源: 互联网 发布时间: 2014-02-18
Java--UDP通信(双向)
新建两个工程,一个Send,一个Receive,先启动Receive,再启动Send,至此就可以双向通信
Send工程:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Send {
//接收消息的端口
private static final int PORT_RECEIVE = 8080;
public static void main(String[] args) {
handler();
}
private static void handler() {
DatagramSocket sendSocket = null;
DatagramSocket receiveSocket = null;
try {
//实例化两个套接字,一个用于接收消息,一个用于发送消息
sendSocket = new DatagramSocket();
receiveSocket = new DatagramSocket(PORT_RECEIVE);
//实例化线程并启动,一个用于接收消息,一个用于发送消息
new Thread(new SendThread(sendSocket)).start();
new Thread(new ReceiveThread(receiveSocket)).start();
} catch (SocketException e) {
System.out.println("handler:异常!");
}
}
}
/*
* 发送消息的线程类
*/
class SendThread implements Runnable{
//将消息发送到指定端口和地址
private static final int PORT_SEND = 9090;
private static final String IP_SEND = "localhost";
private DatagramSocket sendSocket;
public SendThread(DatagramSocket sendSocket) {
this.sendSocket = sendSocket;
}
@Override
public void run() {
BufferedReader br = null;
try {
while(true){
//键盘录入
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
//将键盘录入的内容转换成字节数组
byte[] buf = line.getBytes();
//实例化一个数据包,指定发送的内容,内容长度,发送的地址和端口
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(IP_SEND), PORT_SEND);
//发送数据包
sendSocket.send(dp);
//打印发送的内容
System.out.println("I:" + line);
}
}
} catch (IOException e) {
System.out.println("send fail!");
}finally{
if(sendSocket != null){
sendSocket.close();
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*
* 接收消息的线程类
*/
class ReceiveThread implements Runnable{
private DatagramSocket receiveSocket;
public ReceiveThread(DatagramSocket receiveSocket) {
this.receiveSocket = receiveSocket;
}
@Override
public void run() {
try {
while(true){
//一次接收的内容的最大容量
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收数据包
receiveSocket.receive(dp);
//取得数据包里的内容
String data = new String(dp.getData(), 0, dp.getLength());
//打印接收到的数据
System.out.println("Other:" + data);
}
} catch (IOException e) {
System.out.println("receive fail");
}finally{
if(receiveSocket != null){
receiveSocket.close();
}
}
}
}
Receive工程:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Receive {
//接收消息的端口
private static final int PORT_RECEIVE = 9090;
public static void main(String[] args) {
handler();
}
private static void handler() {
DatagramSocket sendSocket = null;
DatagramSocket receiveSocket = null;
try {
//实例化两个套接字,一个用于接收消息,一个用于发送消息
sendSocket = new DatagramSocket();
receiveSocket = new DatagramSocket(PORT_RECEIVE);
//实例化线程并启动,一个用于接收消息,一个用于发送消息
new Thread(new SendThread(sendSocket)).start();
new Thread(new ReceiveThread(receiveSocket)).start();
} catch (SocketException e) {
System.out.println("handler:异常!");
}
}
}
/*
* 发送消息的线程类
*/
class SendThread implements Runnable{
//将消息发送到指定端口和地址
private static final int PORT_SEND = 8080;
private static final String IP_SEND = "localhost";
private DatagramSocket sendSocket;
public SendThread(DatagramSocket sendSocket) {
this.sendSocket = sendSocket;
}
@Override
public void run() {
BufferedReader br = null;
try {
while(true){
//键盘录入
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
//将键盘录入的内容转换成字节数组
byte[] buf = line.getBytes();
//实例化一个数据包,指定发送的内容,内容长度,发送的地址和端口
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(IP_SEND), PORT_SEND);
//发送数据包
sendSocket.send(dp);
//打印发送的内容
System.out.println("I:" + line);
}
}
} catch (IOException e) {
System.out.println("send fail");
}finally{
if(sendSocket != null){
sendSocket.close();
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*
* 接收消息的线程类
*/
class ReceiveThread implements Runnable{
private DatagramSocket receiveSocket;
public ReceiveThread(DatagramSocket receiveSocket) {
this.receiveSocket = receiveSocket;
}
@Override
public void run() {
try {
while(true){
//一次接收的内容的最大容量
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收数据包
receiveSocket.receive(dp);
String data = new String(dp.getData(), 0, dp.getLength());
//取得数据包里的内容
System.out.println("Other:" + data);
}
} catch (IOException e) {
System.out.println("receive fail");
}finally{
if(receiveSocket != null){
receiveSocket.close();
}
}
}
}
[2] Java-Socket通讯(双向,有界面)
来源: 互联网 发布时间: 2014-02-18
Java--Socket通信(双向,有界面)
服务端:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class ChatFrameServer{
private PrintWriter pw;
private JFrame frame;
private JPanel pane_buttom;
private JSplitPane pane_center;
//显示内容的文本框,输入内容的文本框,发送内容按钮
private JScrollPane pane_showWindow;
private JScrollPane pane_inputWindow;
private JTextArea area_showWindow;
private JTextArea area_inputWindow;
private JButton btn_send;
private Dimension dimension;//用于设置area_showWindow可拖拉的大小
//初始化
public ChatFrameServer() {
frame = new JFrame();
pane_buttom = new JPanel();
pane_showWindow = new JScrollPane();
pane_inputWindow = new JScrollPane();
area_showWindow = new JTextArea();
area_inputWindow = new JTextArea();
pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, pane_showWindow, pane_inputWindow);
btn_send = new JButton("发送");
dimension = new Dimension(50, 300);
}
//调用方法显示窗口
public void showFrame(){
initFrame();
initChatTextArea();
initButton();
btn_send();
socket();
}
//主窗体
public void initFrame(){
frame.setTitle("服务端");
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setBounds(width / 2, height / 2, 400, 450);
frame.setVisible(true);
}
//内容显示文本框和输入内容文本框
private void initChatTextArea(){
//取得视图焦点
pane_showWindow.getViewport().add(area_showWindow);
pane_inputWindow.getViewport().add(area_inputWindow);
//将显示文本域设置为不可编辑
area_showWindow.setEditable(false);
//设置显示文本域可拖拉的大小
pane_showWindow.setMinimumSize(dimension);
frame.add(pane_center, BorderLayout.CENTER);
}
//发送文件,发送内容按钮
public void initButton(){
pane_buttom.add(btn_send);
frame.add(pane_buttom, BorderLayout.SOUTH);
}
private void btn_send(){
btn_send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String info = area_inputWindow.getText();
area_showWindow.append("服务端:"+info+"\r\n");
pw.println(info);
area_inputWindow.setText("");
}
});
}
private void socket(){
ServerSocket ss;
try {
ss = new ServerSocket(9988);
//等待连接 客户端
Socket s=ss.accept();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
//PrintWriter必须和socket有密切的关系
pw=new PrintWriter(s.getOutputStream(),true);
//读取从客户端法发来的信息
while(true) {
//读取从客户端发来的信息
String info=br.readLine();
//在文本栏里显示
area_showWindow.append("客户端:"+info+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatFrameServer chat = new ChatFrameServer();
chat.showFrame();
}
}
客户端:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class ChatFrame{
private PrintWriter pw;
private JFrame frame;
private JPanel pane_buttom;
private JSplitPane pane_center;
//显示内容的文本框,输入内容的文本框,发送内容按钮
private JScrollPane pane_showWindow;
private JScrollPane pane_inputWindow;
private JTextArea area_showWindow;
private JTextArea area_inputWindow;
private JButton btn_send;
private Dimension dimension;//用于设置area_showWindow可拖拉的大小
//初始化
public ChatFrame() {
frame = new JFrame();
pane_buttom = new JPanel();
pane_showWindow = new JScrollPane();
pane_inputWindow = new JScrollPane();
area_showWindow = new JTextArea();
area_inputWindow = new JTextArea();
pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, pane_showWindow, pane_inputWindow);
btn_send = new JButton("发送");
dimension = new Dimension(50, 300);
}
//调用方法显示窗口
public void showFrame(){
initFrame();
initChatTextArea();
initButton();
btn_send();
socket();
}
//主窗体
public void initFrame(){
frame.setTitle("客户端");
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setBounds(width / 2, height / 2, 400, 450);
frame.setVisible(true);
}
//内容显示文本框和输入内容文本框
private void initChatTextArea(){
//取得视图焦点
pane_showWindow.getViewport().add(area_showWindow);
pane_inputWindow.getViewport().add(area_inputWindow);
//将显示文本域设置为不可编辑
area_showWindow.setEditable(false);
//设置显示文本域可拖拉的大小
pane_showWindow.setMinimumSize(dimension);
frame.add(pane_center, BorderLayout.CENTER);
}
//发送文件,发送内容按钮
public void initButton(){
pane_buttom.add(btn_send);
frame.add(pane_buttom, BorderLayout.SOUTH);
}
private void btn_send(){
btn_send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String info = area_inputWindow.getText();
area_showWindow.append("客户端: "+info+"\r\n");
pw.println(info);
area_inputWindow.setText("");
}
});
}
private void socket(){
try {
Socket s = new Socket("127.0.0.1",9988);
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
while(true){
//不停地读取从服务器端发来的信息
String info=br.readLine();
area_showWindow.append("服务端: "+info+"\r\n");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatFrame chat = new ChatFrame();
chat.showFrame();
}
}
[3] 您正在使用的移动电话已经 40 岁
来源: 互联网 发布时间: 2014-02-18
你正在使用的移动电话已经 40 岁
嗨 Joel,我是 Marty。我正在用手机给你打电话,是一部真正的移动手持电话哦。
1973 年 4 月 3 日,摩托罗拉的工程师 Marty Cooper 首次使用手机打通电话。首次通话献给了当时的竞争对手,Bell Labs 研发中心的负责人 Joel Engel。至于设备,使用的是摩托罗拉的 DynaTAC 8000x,重达 2.5 英镑,比我们今天使用的手机巨大得多。
Marty Cooper 作为移动电话的发明者,现在还在为美国联邦通信委员会(FCC)和国家电信和信息管理局(NTIA)工作,他现在所使用的手机还是摩托罗拉——Droid RAZR。
最新技术文章: