java编写通信代码 java通讯教程

如何使用JAVA编写网络通信程序

使用JAVA来进行网络编程

创新互联是一家专业提供扶余企业网站建设,专注与成都做网站、成都网站建设、成都h5网站建设、小程序制作等业务。10年已为扶余众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

1)

由于客户端通过IE同服务器建立联系,所以客户端使用Applet,服务器端使用Application;

2)

服务器应设置成多线程,应答多个客户的请求;

3)

两端通信使用SOCKET机制。

2.2客户机/服务器通信的动机

客户机-服务器范例的基本动机来自聚集问题。

为了理解这一问题,设想一个人试图在分离的机器上启动两个程序并让它们进行通

信,还要记住,计算机的运行要比人的速度快许多数量级。在某人启动第一个程序后,

该程序开始执行并向其对等程序发送消息,在几个微妙内,它便发现对等程序还不存在,

于是就发出一条错误消息,然后退出。在这时,某个人启动了第二个程序,不幸的是,当第二个程序开始执行时,它发现对等程序已经终止执行了,即便是两个程序连续重新试着通信,

但由于它们每个程序执行的速度那么快。因而它们

在同一瞬间向对方发送消息的概率是很低的。

客户机-服务器模型是这样解决这种聚集问题的。它要求在任何一对进行通信的应用进程中,有一方必须在启动执行后(无限期地)等待对方的联系,这种解决方案是重要的,因为TCP/IP自己对入呼叫通信是不会响应的。

由于TCP/IP并不是提供在一个报文到达后自动创建运行程序的任何机制,因此一个程序必须在任一请求到来前就在那里等待以接受通信。

因此,为确保计算机已准备好了进行通信,多数系统管理员都安排通信程序在操作系统引导时就自动启动。每个程序都一直运行下去,以便等待下一个服务请求的到来(此服务是该程序提供的)

望采纳!!!

用java编写一个能进行简单TCP/IP通信的C/S程序

import java.net.ServerSocket;

import java.net.Socket;

public class TcpServer

{

public static void main(String[] args) throws Exception

{

// 创建服务器端的socket对象

ServerSocket ss = new ServerSocket(5000);

// 监听连接

Socket socket = ss.accept();

// 直到连接建立好之后代码才会往下执行

System.out.println("Connected Successfully!");

}

}

import java.net.Socket;

public class TcpClient

{

public static void main(String[] args) throws Exception

{

Socket socket = new Socket("127.0.0.1", 5000);

}

}

java实现串口通信代码

public static void process() {

try {

Enumeration portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements())

{

CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称

{

if(portId.getName().equals("COM1")){//如果是COM1端口则退出循环

break;

}else{

portId=null;

}

}

}

SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打开串口的超时时间为1000ms

serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600,数据位8位,停止位1们,奇偶校验无

InputStream in = serialPort.getInputStream();//得到输入流

OutputStream out = serialPort.getOutputStream();//得到输出流

//进行输入输出操作

//操作结束后

in.close();

out.close();

serialPort.close();//关闭串口

} catch (PortInUseException e) {

e.printStackTrace();

} catch (UnsupportedCommOperationException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

java通讯录管理系统设计代码怎么编译

java编写这个通讯录管理系统

java编写这个通讯录管理系统_Java如何实现通讯录管理系统

咕噜噜在芬兰

原创

关注

3点赞·2305人阅读

Java如何实现通讯录管理系统

发布时间:2020-07-28 09:39:42

来源:亿速云

阅读:65

作者:Leah

这篇文章将为大家详细讲解有关Java如何实现通讯录管理系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下

完成项目的流程:

1.根据需求,确定大体方向

2.功能模块分析

3.界面实现

4.功能模块设计

5.coding

6.代码测试

下面是源代码:import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.concurrent.SynchronousQueue;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.text.html.HTMLDocument.Iterator;

class Infro{

public String id;

public String name;

public String sex;

public String address;

public String e_mail;

public String phoneNumber;

static int index = 0;

static ArrayList list = new ArrayList();

static int len = list.size();

//构造函数

public Infro(String id,String name,String sex,String address,String e_mail,String phoneNumber){

this.id = id;

this.name = name;

this.sex = sex;

this.address = address;

this.e_mail = e_mail;

this.phoneNumber = phoneNumber;

}

public String toString(){

return "编号:"+id+" 姓名:"+name+" 性别:"+sex+" 通讯地址:"+address+" 邮箱地址:"+e_mail+" 电话:"+phoneNumber;

}

/**

* 添加功能

**/

public static void addFunction(){//添加功能

Infro infro = new Infro("","","","","","");

System.out.println("请输入添加的数据:");

Scanner in = new Scanner(System.in);

System.out.println("输入编号:");

infro.id = in.next();

System.out.println("输入姓名:");

infro.name = in.next();

System.out.println("输入性别:");

infro.sex = in.next();

System.out.println("输入通讯地址:");

infro.address = in.next();

System.

out.println("输入邮箱地址:");

infro.e_mail = in.next();

System.out.println("输入电话:");

infro.phoneNumber = in.next();

list.add(index,infro);

index++;

if(list.isEmpty()){

System.out.println("数据添加失败啦");

}else{

System.out.println("数据添加成功啦");

len++;//list集合长度加一

// System.out.println(list.get(0).toString());

}

}

// public static void deleteFunction(){//删除功能

// System.out.println("输入要删除的联系人的编号");

// Scanner in_2 = new Scanner(System.in);

// String d1 = in_2.nextLine();

// for(int a= 0; a

// if(d1.equals(list.get(a).id)){

// list.remove(list.get(a));

// len --;

// }

// }

// }

/**

* 删除功能

**/

public static void deleteFunction(){

System.out.println("输入要删除的联系人的编号");

Scanner in_2 = new Scanner(System.in);

String d1 = in_2.nextLine();

java.util.Iterator it = list.iterator();

while (it.hasNext()){

Infro infro = it.next();

if (infro.id.equals(d1)){

it.remove();

--index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)

System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);

}

}

}

/**

* 修改功能

**/

public static void reditFunction(){

System.out.println("输入要修改的通讯录的Id");

Scanner in_r = new Scanner(System.in);

String r1 = in_r.nextLine();

for(int a = 0; a len;a++){

if(r1.equals(list.get(a).id)){

System.out.println("输入修改后的姓名:");

String name_1 = in_r.next();

list.get(a).name = name_1;

System.out.println("输入修改后的性别:");

String sex_1 = in_r.next();

list.get(a).sex = sex_1;

System.out.println("输入修改后的通讯地址:");

String address_1 = in_r.next();

list.get(a).address = address_1;

System.out.println("输入修改后的邮箱地址:");

String e_mail_1 = in_r.next();

list.get(a).e_mail = e_mail_1;

System.out.println("输入修改后的电话:");

String phoneNumber_1 = in_r.next();

list.get(a).phoneNumber = phoneNumber_1;

System.out.println("数据修改完毕");

}

}

}

/**

* 查询功能

**/

public static void searchFunction() throws Exception{//查询功能

System.out.println("请输入要查询的姓名:");

Scanner in_1 = new Scanner(System.in);

String s1=in_1.nextLine();

for(int a= 0; a

if(s1.equals(list.get(a).name)){

System.out.println(list.get(a).toString());

}

}

}

/**

* 显示功能

**/

public static void showFunction(){

for(int i = 0 ;i

System.out.println(list.get(i).toString());

}

}

/**

* 保存功能

**/

public static void writeFunction() throws IOException{

FileWriter writer = new FileWriter("通讯录管理.txt");

for(int i = 0 ;i

String []strwriter = new String[len];

strwriter[i]=list.get(i).toString();

writer.write(strwriter[i]);

writer.write("\r\n");

System.out.println("成功写入一行数据到 通讯录管理.txt 中");

}

writer.close();//关闭写入流,释放资源

}

/**

* 读取功能

**/

public static void readFunction() throws IOException{

FileReader reader = new FileReader("通讯录管理.txt");

BufferedReader br = new BufferedReader(reader);

String str;

while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾

System.out.println(str);

}

br.close();

}

}

public class Demo extends JFrame {

/**

* 界面设计

**/

public Demo(){

Container c = getContentPane(); //定义一个顶级容器c

JPanel jp = new JPanel(); //新建JPanel面板--jp

JButton button1 = new JButton("新建联系人");

JButton button2 = new JButton("删除联系人");

JButton button3 = new JButton("编辑联系人");

JButton button4 = new JButton("查找联系人");

JButton button5 = new JButton("显示所有联系人");

JButton button6 = new JButton("保存联系人到本地");

JButton button7 = new JButton("读取本地联系人");

jp.setLayout(new GridLayout(2,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)

jp.add(button1);

jp.add(button2);

jp.add(button3);

jp.add(button4);

jp.add(button5);

jp.add(button6);

jp.add(button7);

c.add(jp);//将JPanel面板jp添加到顶级容器c中

setSize(600,500);

setTitle("*通 讯 录 管 理 系 统*");

setVisible(true);

setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

/**

*按键响应

*

**/

button1.addActionListener(new ActionListener(){//添加功能实现

public void actionPerformed(ActionEvent arg0){

Infro.addFunction();

}

});

button2.addActionListener(new ActionListener(){//删除功能实现

public void actionPerformed(ActionEvent arg0){

Infro.deleteFunction();

}

});

button3.addActionListener(new ActionListener(){//修改功能实现

public void actionPerformed(ActionEvent arg0){

Infro.reditFunction();

}

});

button4.addActionListener(new ActionListener(){//查询功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.searchFunction();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

button5.addActionListener(new ActionListener(){//显示功能实现

public void actionPerformed(ActionEvent arg0){

Infro.showFunction();

}

});

button6.addActionListener(new ActionListener(){//保存功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.writeFunction();

} catch (IOException e) {

e.printStackTrace();

}

}

});

button7.addActionListener(new ActionListener(){//读取功能实现

public void actionPerformed(ActionEvent arg0){

try {

Infro.readFunction();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Demo();

Infro a = new Infro("", "", "", "", "", "");

}

}

关于Java如何实现通讯录管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

新闻标题:java编写通信代码 java通讯教程
本文网址:https://www.cdcxhl.com/article10/dddhggo.html

成都网站建设公司_创新互联,为您提供网站设计公司定制网站网站内链网站设计企业网站制作网页设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化