大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java网络通讯代码 java 网络通信框架

JAVA通讯录 求一个JAVA编写的通讯录,基本的就可以。

具体方法如下:

成都创新互联专业为企业提供渝水网站建设、渝水做网站、渝水网站设计、渝水网站制作等企业网站建设、网页设计与制作、渝水企业网站模板建站服务,十多年渝水做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

1、定义封装一条记录的实体类

2、根据实际系统容量,定义一个数组

3、完成系统中显示全部记录的逻辑

4、完成系统中添加一条记录的逻辑

5、完成系统中删除一条记录的逻辑

6、完成系统中修改一条记录的逻辑

7、全部代码:

import java.util.Scanner;

class Contact {

String cellPhone;

String name;

}

public class Main {

private static void menu () {

System.out.println("************** 菜单 ******"

+ "************");

System.out.println(" 1.显示全部通讯录");

System.out.println(" 2.增加一条记录");

System.out.println(" 3.删除一条记录");

System.out.println(" 4.修改一条记录");

System.out.println(" 0.退出");

}

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

Contact[] contacts = new Contact[200];

int size = 0;

String cmd = "";

do {

menu();

System.out.print("请输入你得选择:(0-4)");

cmd = scn.nextLine();

if (cmd.equals("1")) {

if (size == 0)

System.out.println("系统当前无记录!");

else

for (int i = 0; i size; i++) {

System.out.println(contacts[i].name + ":"

+ contacts[i].cellPhone);

}

} else if (cmd.equals("2")) {

System.out.print("请输入手机号:");

String cellphone = scn.nextLine();

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

String name = scn.nextLine();

Contact contact = new Contact();

contact.cellPhone = cellphone;

contact.name = name;

if (size contacts.length) {

contacts[size++] = contact;

System.out.println("添加成功!");

} else {

System.out.println("你最多只能添加" +

contacts.length + "条记录");

}

} else if (cmd.equals("3")) {

System.out.print("请输入要删除的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

for (int i = index; i size; i++) {

contacts[index] = contacts[index + 1];

}

contacts[size - 1] = null;

size--;

System.out.println("删除成功!");

}

} else if (cmd.equals("4")) {

System.out.print("请输入要修改的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

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

String name = scn.nextLine();

contacts[index].name = name;

}

}

} while (!cmd.equals("0"));

System.out.println("退出成功!");

scn.close();

System.exit(0);

}

}

Java语言写段简单,但又有技术含量的即时通讯代码,不胜感激之情溢于满天下

这里有一个简单的模拟通讯 要先运行服务器端 再运行客户端 否则会报错:

服务器端代码:

package com.test3;

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Server2 extends JFrame implements ActionListener , KeyListener {

JTextArea jta=null;

JScrollPane jsp=null;

JTextField jtf=null;

JButton jb=null;

JPanel jp=null;

InputStreamReader isr=null;

BufferedReader br=null;

PrintWriter pw=null;

Socket s;

String jtatext="";

public static void main(String[] args) {

// TODO Auto-generated method stub

Server2 sv2=new Server2();

}

public Server2(){

jta=new JTextArea();

jta.setEditable(false);

jsp=new JScrollPane(jta);

jtf=new JTextField(10);

jtf.addKeyListener(this);

jb=new JButton("发送");

jb.addActionListener(this);

jp=new JPanel();

jp.add(jtf);

jp.add(jb);

this.add(jsp,"Center");

this.add(jp,"South");

this.setSize(300,300);

this.setLocationRelativeTo(this);

this.setTitle("服务器端");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

try{

ServerSocket ss=new ServerSocket(9999);

s=ss.accept();

isr=new InputStreamReader(s.getInputStream());

br=new BufferedReader(isr);

pw=new PrintWriter(s.getOutputStream(),true);

while(true){

String info=br.readLine();

jta.append("客户端对服务器说:   "+info+"\r\n");

// this.jta.setText(jtatext);

}

}catch(Exception e){

e.printStackTrace();

}

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jb){

try {

pw.println(jtf.getText());

jta.append("服务器对客户端说:   "+jtf.getText()+"\r\n");

// jta.setText(jtatext);

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

@Override

public void keyTyped(KeyEvent e) {}

@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

try {

pw.println(jtf.getText());

jta.append("服务器对客户端说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

@Override

public void keyReleased(KeyEvent e) {}

}

客户端代码:

package com.test3;

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Client2 extends JFrame implements ActionListener ,KeyListener {

JTextArea jta=null;

JScrollPane jsp=null;

JTextField jtf=null;

JButton jb=null;

JPanel jp=null;

String jtatext="";

Socket s;

PrintWriter pw=null;

InputStreamReader isr=null;

BufferedReader br=null;

public static void main(String[] args) {

// TODO Auto-generated method stub

Client2 sv2=new Client2();

}

public Client2(){

jta=new JTextArea();

jta.setEditable(false);

jsp=new JScrollPane(jta);

jtf=new JTextField(10);

jtf.addKeyListener(this);

jb=new JButton("发送");

jb.addActionListener(this);

jp=new JPanel();

jp.add(jtf);

jp.add(jb);

this.add(jsp,"Center");

this.add(jp,"South");

this.setSize(300,300);

this.setLocationRelativeTo(this);

this.setTitle("客户端");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

try {

 s=new Socket("127.3.3.3",9999);

 isr=new InputStreamReader(s.getInputStream());

 br=new BufferedReader(isr);

 pw=new PrintWriter(s.getOutputStream(),true);

 

 while(true){

 String info=br.readLine(); 

jta.append("服务器对客户端说:   "+info+"\r\n");

 

 }

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jb){

try {

pw.println(this.jtf.getText());

jta.append("客户端对服务器说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

try {

pw.println(this.jtf.getText());

jta.append("客户端对服务器说:   "+jtf.getText()+"\r\n");

jtf.setText("");

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

public void keyReleased(KeyEvent e) {}

}

JAVA程序在广域网上如何实现通信

一般可以使用socket或者RMI编程进行通信,推荐使用RMI,因为比较简单,给你看看rmi通信的例子

1.接口Hello代码

======================================================================

import java.rmi.*;

public interface Hello extends Remote {

public String sayHello(String s) throws RemoteException;

}

2.实现类HelloImpl代码

======================================================================

import java.rmi.*;

import java.rmi.server.*;

public class HelloImpl extends UnicastRemoteObject implements Hello {

public HelloImpl() throws RemoteException{}

public String sayHello(String s) throws RemoteException {

return "Echo:"+s;

}

}

3.服务器类RMIServer代码

======================================================================

import java.rmi.*;

class RMIServer {

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

HelloImpl hi=new HelloImpl();

Naming.rebind("hello",hi);

}

}

4.客户端类RMIClient代码

======================================================================

import java.rmi.*;

class RMIClient {

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

Hello h=(Hello)Naming.lookup("hello");

System.out.println(h.sayHello("Hello World!"));

}

}

5.编译

======================================================================

javac *.java 编译所有.java文件

rmic HelloImpl 产生stub,Skeleton文件,Stub是远程对象的客户端代理

是远程对象的服务器代理

6.运行

======================================================================

打开一个dos窗口,输入rmiregistry

打开另一个dos窗口,输入java RMIServer,启动服务器

打开最后一个dos窗口,输入java RMIClient,启动客户端

又可以看见Hello World了

java通讯录全部代码!

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class AddList {

private String filePath = "";

private String bakPath = "";

private String content = "";

Scanner sc = new Scanner(System.in);

public String readFile(){

content = "";

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileReader fr = null;

try {

if (file.exists()) {

fr = new FileReader(file);

char[] chars = new char[1024];

int n = 0;

while((n = fr.read(chars)) != -1){

String string = new String(chars, 0, n);

content = content + string;

}

} else {

System.out.println("文件不存在");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fr != null) {

try {

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return content;

}

public void writeFile(String path){

File file = new File(path);

FileOutputStream fos = null;

mkDirs(path);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void writeFile(){

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileOutputStream fos = null;

mkDirs(filePath);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void mkDirs(String filepath){

if (filepath.indexOf("\\") != -1) {

filepath = filepath.replaceAll("\\", "/");

}

int n = filepath.indexOf("//");

String path = filepath.substring(0, n) + "//";

filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());

String[] files = filepath.split("/");

for (int i = 0; i files.length - 1; i++) {

path = path + files[i];

File file = new File(path);

if (!file.exists()) {

file.mkdir();

}

}

}

public void addImfor(){

System.out.println("--------增加记录---------");

String name = "";

String tel = "";

String email = "";

content = readFile();

while(true){

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

name = sc.next();

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

tel = sc.next();

System.out.println("Email:");

email = sc.next();

content = content + name + "" + tel + "" + email +"==";

System.out.println("0、Exit 1、继续");

int i = sc.nextInt();

if (i == 0) {

break;

}

}

writeFile();

}

public void deleteImfor(){

System.out.println("---------删除记录---------");

String name = "";

String[] imfors = null;

content = readFile();

while(true){

System.out.println("你要删除的姓名是:");

name = sc.next();

if (content.indexOf(name) != -1) {

imfors = content.split("==");

for (int i = 0; i imfors.length; i++) {

if (imfors[i].indexOf(name) != -1) {

imfors[i] = "";

}

}


网站栏目:java网络通讯代码 java 网络通信框架
链接地址:http://dzwzjz.com/article/ddcohdh.html
在线咨询
服务热线
服务热线:028-86922220
TOP