资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JAVA网络代码,JAVA 网络

Java的网络连接代码如何测试?

如果是在局域网中测试,本地的话就是127.0.0.1或localhost,或改为局域网中的其它IP地址,如果是广域网的话,那就得搭建服务器,就是不搭建也得有一个固定的外网IP才行。

成都创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为万宁企业提供专业的成都做网站、成都网站建设,万宁网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

关于java网络编程的基础问题,已经附带源代码

你需要用一个list 管理 所有的客户端socket 。

Socket socket = ss.accept();

socketList.add(socket )

然后 再依次向么个socket 输出

for (Iterator iterator = socketList.iterator(); iterator.hasNext();) {

Socket object = (Socket) iterator.next();

OutputStream os = socket.getOutputStream();

DataOutputStream dis = new DataOutputStream(os);

dis.writeUTF( “message”);

}

java代码TCP/IP网络通信服务器客户端,实现双方信息交互。

package com.weixin.test;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;

import org.junit.Test;

public class ScoketTest {

@Test

public void client() throws Exception{

InetAddress i=InetAddress.getByName("127.0.0.1");

Socket s=new Socket(i, 9000);

OutputStream outputStream = s.getOutputStream();

outputStream.write("服务端你好,我是客户端哦!".getBytes());

s.shutdownOutput();

InputStream inputStream=s.getInputStream();

int length=0;

byte[] bytes=new byte[1024];

while ((length=inputStream.read(bytes))!=-1) {

System.err.println(new String(bytes,0,length));

}

inputStream.close();

outputStream.close();

s.close();

}

@Test

public void server() throws Exception{

ServerSocket serverSocket=new ServerSocket(9000);

Socket socket = serverSocket.accept();

InputStream inputStream = socket.getInputStream();

OutputStream outputStream = socket.getOutputStream();

int length=0;

byte[] bytes=new byte[1024];

while ((length=inputStream.read(bytes))!=-1) {

System.err.println(new String(bytes, 0,length));

}

outputStream.write("客户端你好,本王已收到!".getBytes());

outputStream.close();

inputStream.close();

socket.close();

serverSocket.close();

}

}

如何用70行Java代码实现深度神经网络算法

import java.util.Random;

public class BpDeep{

public double[][] layer;//神经网络各层节点

public double[][] layerErr;//神经网络各节点误差

public double[][][] layer_weight;//各层节点权重

public double[][][] layer_weight_delta;//各层节点权重动量

public double mobp;//动量系数

public double rate;//学习系数

public BpDeep(int[] layernum, double rate, double mobp){

this.mobp = mobp;

this.rate = rate;

layer = new double[layernum.length][];

layerErr = new double[layernum.length][];

layer_weight = new double[layernum.length][][];

layer_weight_delta = new double[layernum.length][][];

Random random = new Random();

for(int l=0;llayernum.length;l++){

layer[l]=new double[layernum[l]];

layerErr[l]=new double[layernum[l]];

if(l+1layernum.length){

layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];

layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];

for(int j=0;jlayernum[l]+1;j++)

for(int i=0;ilayernum[l+1];i++)

layer_weight[l][j][i]=random.nextDouble();//随机初始化权重

}

}

}

//逐层向前计算输出

public double[] computeOut(double[] in){

for(int l=1;llayer.length;l++){

for(int j=0;jlayer[l].length;j++){

double z=layer_weight[l-1][layer[l-1].length][j];

for(int i=0;ilayer[l-1].length;i++){

layer[l-1][i]=l==1?in[i]:layer[l-1][i];

z+=layer_weight[l-1][i][j]*layer[l-1][i];

}

layer[l][j]=1/(1+Math.exp(-z));

}

}

return layer[layer.length-1];

}

//逐层反向计算误差并修改权重

public void updateWeight(double[] tar){

int l=layer.length-1;

for(int j=0;jlayerErr[l].length;j++)

layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);

while(l--0){

for(int j=0;jlayerErr[l].length;j++){

double z = 0.0;

for(int i=0;ilayerErr[l+1].length;i++){

z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;

layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整

layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整

if(j==layerErr[l].length-1){

layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整

layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整

}

}

layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差

}

}

}

public void train(double[] in, double[] tar){

double[] out = computeOut(in);

updateWeight(tar);

}

}

参考资料

求java网络聊天室(B/S模式)程序代码

共四个java文件,源代码如下:

import java.awt.*;

import java.net.*;

import java.awt.event.*;

import java.io.*;

import java.util.Hashtable;

public class ChatArea extends Panel implements ActionListener,Runnable

{

Socket socket=null;

DataInputStream in=null;

DataOutputStream out=null;

Thread threadMessage=null;

TextArea 谈话显示区,私聊显示区=null;

TextField 送出信息=null;

Button 确定,刷新谈话区,刷新私聊区;

Label 提示条=null;

String name=null;

Hashtable listTable;

List listComponent=null;

Choice privateChatList;

int width,height;

public ChatArea(String name,Hashtable listTable,int width,int height)

{

setLayout(null);

setBackground(Color.orange);

this.width=width;

this.height=height;

setSize(width,height);

this.listTable=listTable;

this.name=name;

threadMessage=new Thread(this);

谈话显示区=new TextArea(10,10);

私聊显示区=new TextArea(10,10);

确定=new Button("送出信息到:");

刷新谈话区=new Button("刷新谈话区");

刷新私聊区=new Button("刷新私聊区");

提示条=new Label("双击聊天者可私聊",Label.CENTER);

送出信息=new TextField(28);

确定.addActionListener(this);

送出信息.addActionListener(this);

刷新谈话区.addActionListener(this);

刷新私聊区.addActionListener(this);

listComponent=new List();

listComponent.addActionListener(this);

privateChatList=new Choice();

privateChatList.add("大家(*)");

privateChatList.select(0);

add(谈话显示区);

谈话显示区.setBounds(10,10,(width-120)/2,(height-120));

add(私聊显示区);

私聊显示区.setBounds(10+(width-120)/2,10,(width-120)/2,(height-120));

add(listComponent);

listComponent.setBounds(10+(width-120),10,100,(height-160));

add(提示条);

提示条.setBounds(10+(width-120),10+(height-160),110,40);

Panel pSouth=new Panel();

pSouth.add(送出信息);

pSouth.add(确定);

pSouth.add(privateChatList);

pSouth.add(刷新谈话区);

pSouth.add(刷新私聊区);

add(pSouth);

pSouth.setBounds(10,20+(height-120),width-20,60);

}

public void setName(String s)

{

name=s;

}

public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)

{

this.socket=socket;

this.in=in;

this.out=out;

try

{

threadMessage.start();

}

catch(Exception e)

{

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==确定||e.getSource()==送出信息)

{

String message="";

String people=privateChatList.getSelectedItem();

people=people.substring(0,people.indexOf("("));

message=送出信息.getText();

if(message.length()0)

{

try {

if(people.equals("大家"))

{

out.writeUTF("公共聊天内容:"+name+"说:"+message);

}

else

{

out.writeUTF("私人聊天内容:"+name+"悄悄地说:"+message+"#"+people);

}

}

catch(IOException event)

{

}

}

}

else if(e.getSource()==listComponent)

{

privateChatList.insert(listComponent.getSelectedItem(),0);

privateChatList.repaint();

}

else if(e.getSource()==刷新谈话区)

{

谈话显示区.setText(null);

}

else if(e.getSource()==刷新私聊区)

{

私聊显示区.setText(null);

}

}

public void run()

{

while(true)

{

String s=null;

try

{

s=in.readUTF();

if(s.startsWith("聊天内容:"))

{

String content=s.substring(s.indexOf(":")+1);

谈话显示区.append("\n"+content);

}

if(s.startsWith("私人聊天内容:"))

{

String content=s.substring(s.indexOf(":")+1);

私聊显示区.append("\n"+content);

}

else if(s.startsWith("聊天者:"))

{

String people=s.substring(s.indexOf(":")+1,s.indexOf("性别"));

String sex=s.substring(s.indexOf("性别")+2);

listTable.put(people,people+"("+sex+")");

listComponent.add((String)listTable.get(people));

listComponent.repaint();

}

else if(s.startsWith("用户离线:"))

{

String awayPeopleName=s.substring(s.indexOf(":")+1);

listComponent.remove((String)listTable.get(awayPeopleName));

listComponent.repaint();

谈话显示区.append("\n"+(String)listTable.get(awayPeopleName)+"离线");

listTable.remove(awayPeopleName);

}

Thread.sleep(5);

}

catch(IOException e)

{

listComponent.removeAll();

listComponent.repaint();

listTable.clear();

谈话显示区.setText("和服务器的连接已中断\n必须刷新浏览器才能再次聊天");

break;

}

catch(InterruptedException e)

{

}

}

}

}

ChatServer.java

import java.io.*;

import java.net.*;

import java.util.*;

public class ChatServer

{

public static void main(String args[])

{

ServerSocket server=null;

Socket you=null;

Hashtable peopleList;

peopleList=new Hashtable();

while(true)

{

try

{

server=new ServerSocket(6666);

}

catch(IOException e1)

{

System.out.println("正在监听");

}

try {

you=server.accept();

InetAddress address=you.getInetAddress();

System.out.println("用户的IP:"+address);

}

catch (IOException e)

{

}

if(you!=null)

{

Server_thread peopleThread=new Server_thread(you,peopleList);

peopleThread.start();

}

else {

continue;

}

}

}

}

class Server_thread extends Thread

{

String name=null,sex=null;

Socket socket=null;

File file=null;

DataOutputStream out=null;

DataInputStream in=null;

Hashtable peopleList=null;

Server_thread(Socket t,Hashtable list)

{

peopleList=list;

socket=t;

try {

in=new DataInputStream(socket.getInputStream());

out=new DataOutputStream(socket.getOutputStream());

}

catch (IOException e)

{

}

}

public void run()

{

while(true)

{ String s=null;

try

{

s=in.readUTF();

if(s.startsWith("姓名:"))

{

name=s.substring(s.indexOf(":")+1,s.indexOf("性别"));

sex=s.substring(s.lastIndexOf(":")+1);

boolean boo=peopleList.containsKey(name);

if(boo==false)

{

peopleList.put(name,this);

out.writeUTF("可以聊天:");

Enumeration enum=peopleList.elements();

while(enum.hasMoreElements())

{

Server_thread th=(Server_thread)enum.nextElement();

th.out.writeUTF("聊天者:"+name+"性别"+sex);

if(th!=this)

{

out.writeUTF("聊天者:"+th.name+"性别"+th.sex);

}

}

}

else

{

out.writeUTF("不可以聊天:");

}

}

else if(s.startsWith("公共聊天内容:"))

{

String message=s.substring(s.indexOf(":")+1);

Enumeration enum=peopleList.elements();

while(enum.hasMoreElements())

{

((Server_thread)enum.nextElement()).out.writeUTF("聊天内容:"+message);

}

}

else if(s.startsWith("用户离开:"))

{

Enumeration enum=peopleList.elements();

while(enum.hasMoreElements())

{ try

{

Server_thread th=(Server_thread)enum.nextElement();

if(th!=thisth.isAlive())

{

th.out.writeUTF("用户离线:"+name);

}

}

catch(IOException eee)

{

}

}

peopleList.remove(name);

socket.close();

System.out.println(name+"用户离开了");

break;

}

else if(s.startsWith("私人聊天内容:"))

{

String 悄悄话=s.substring(s.indexOf(":")+1,s.indexOf("#"));

String toPeople=s.substring(s.indexOf("#")+1);

Server_thread toThread=(Server_thread)peopleList.get(toPeople);

if(toThread!=null)

{

toThread.out.writeUTF("私人聊天内容:"+悄悄话);

}

else

{

out.writeUTF("私人聊天内容:"+toPeople+"已经离线");

}

}

}

catch(IOException ee)

{

Enumeration enum=peopleList.elements();

while(enum.hasMoreElements())

{ try

{

Server_thread th=(Server_thread)enum.nextElement();

if(th!=thisth.isAlive())

{

th.out.writeUTF("用户离线:"+name);

}

}

catch(IOException eee)

{

}

}

peopleList.remove(name);

try

{

socket.close();

}

catch(IOException eee)

{

}

System.out.println(name+"用户离开了");

break;

}

}

}

}

import java.awt.*;

import java.io.*;

import java.net.*;

import java.applet.*;

import java.util.Hashtable;

ClientChat.java

public class ClientChat extends Applet implements Runnable

{

Socket socket=null;

DataInputStream in=null;

DataOutputStream out=null;

InputNameTextField 用户提交昵称界面=null;

ChatArea 用户聊天界面=null;

Hashtable listTable;

Label 提示条;

Panel north, center;

Thread thread;

public void init()

{

int width=getSize().width;

int height=getSize().height;

listTable=new Hashtable();

setLayout(new BorderLayout());

用户提交昵称界面=new InputNameTextField(listTable);

int h=用户提交昵称界面.getSize().height;

用户聊天界面=new ChatArea("",listTable,width,height-(h+5));

用户聊天界面.setVisible(false);

提示条=new Label("正在连接到服务器,请稍等...",Label.CENTER);

提示条.setForeground(Color.red);

north=new Panel(new FlowLayout(FlowLayout.LEFT));

center=new Panel();

north.add(用户提交昵称界面);

north.add(提示条);

center.add(用户聊天界面);

add(north,BorderLayout.NORTH);

add(center,BorderLayout.CENTER);

validate();

}

public void start()

{

if(socket!=nullin!=nullout!=null)

{ try

{

socket.close();

in.close();

out.close();

用户聊天界面.setVisible(false);

}

catch(Exception ee)

{

}

}

try

{

socket = new Socket(this.getCodeBase().getHost(), 6666);

in=new DataInputStream(socket.getInputStream());

out=new DataOutputStream(socket.getOutputStream());

}

catch (IOException ee)

{

提示条.setText("连接失败");

}

if(socket!=null)

{

InetAddress address=socket.getInetAddress();

提示条.setText("连接:"+address+"成功");

用户提交昵称界面.setSocketConnection(socket,in,out);

north.validate();

}

if(thread==null)

{

thread=new Thread(this);

thread.start();

}

}

public void stop()

{

try

{

socket.close();

thread=null;

}

catch(IOException e)

{

this.showStatus(e.toString());

}

}

public void run()

{

while(thread!=null)

{

if(用户提交昵称界面.get能否聊天()==true)

{

用户聊天界面.setVisible(true);

用户聊天界面.setName(用户提交昵称界面.getName());

用户聊天界面.setSocketConnection(socket,in,out);

提示条.setText("祝聊天愉快!");

center.validate();

break;

}

try

{

Thread.sleep(100);

}

catch(Exception e)

{

}

}

}

}

InputNameTextField。java

import java.awt.*;

import java.net.*;

import java.awt.event.*;

import java.io.*;

import java.util.Hashtable;

public class InputNameTextField extends Panel implements ActionListener,Runnable

{

TextField nameFile=null;

String name=null;

Checkbox male=null,female=null;

CheckboxGroup group=null;

Button 进入聊天室=null,退出聊天室=null;

Socket socket=null;

DataInputStream in=null;

DataOutputStream out=null;

Thread thread=null;

boolean 能否聊天=false;

Hashtable listTable;

public InputNameTextField(Hashtable listTable)

{

this.listTable=listTable;

nameFile=new TextField(10);

group=new CheckboxGroup();

male=new Checkbox("男",true,group);

female=new Checkbox("女",false,group);

进入聊天室=new Button("进入");

退出聊天室=new Button("退出");

进入聊天室.addActionListener(this);

退出聊天室.addActionListener(this);

thread=new Thread(this);

add(new Label("昵称:"));

add(nameFile);

add(male);

add(female);

add(进入聊天室);

add(退出聊天室);

退出聊天室.setEnabled(false);

}

public void set能否聊天(boolean b)

{

能否聊天=b;

}

public boolean get能否聊天()

{

return 能否聊天;

}

public String getName()

{

return name;

}

public void setName(String s)

{

name=s;

}

public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)

{

this.socket=socket;

this.in=in;

this.out=out;

try{

thread.start();

}

catch(Exception e)

{

nameFile.setText(""+e);

}

}

public Socket getSocket()

{

return socket;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==进入聊天室)

{

退出聊天室.setEnabled(true);

if(能否聊天==true)

{

nameFile.setText("您正在聊天:"+name);

}

else

{

this.setName(nameFile.getText());

String sex=group.getSelectedCheckbox().getLabel();

if(socket!=nullname!=null)

{

try{

out.writeUTF("姓名:"+name+"性别:"+sex);

}

catch(IOException ee)

{

nameFile.setText("没有连通服务器"+ee);

}

}

}

}

if(e.getSource()==退出聊天室)

{

try

{

out.writeUTF("用户离开:");

}

catch(IOException ee)

{

}

}

}

public void run()

{

String message=null;

while(true)

{

if(in!=null)

{

try

{

message=in.readUTF();

}

catch(IOException e)

{

nameFile.setText("和服务器断开"+e);

}

}

if(message.startsWith("可以聊天:"))

{

能否聊天=true;

break;

}

else if(message.startsWith("聊天者:"))

{

String people=message.substring(message.indexOf(":")+1);

listTable.put(people,people);

}

else if(message.startsWith("不可以聊天:"))

{

能否聊天=false;

nameFile.setText("该昵称已被占用");

}

}

}

}

用java编写 网络爬虫求代码和流程 急

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.regex.*;

import javax.swing.*;

import javax.swing.table.*;//一个Web的爬行者(注:爬行在这里的意思与抓取,捕获相同)

public class SearchCrawler extends JFrame{

//最大URL保存值

private static final String[] MAX_URLS={"50","100","500","1000"};

//缓存robot禁止爬行列表

private HashMap disallowListCache=new HashMap();

//搜索GUI控件

private JTextField startTextField;

private JComboBox maxComboBox;

private JCheckBox limitCheckBox;

private JTextField logTextField;

private JTextField searchTextField;

private JCheckBox caseCheckBox;

private JButton searchButton;

//搜索状态GUI控件

private JLabel crawlingLabel2;

private JLabel crawledLabel2;

private JLabel toCrawlLabel2;

private JProgressBar progressBar;

private JLabel matchesLabel2;

//搜索匹配项表格列表

private JTable table;

//标记爬行机器是否正在爬行

private boolean crawling;

//写日志匹配文件的引用

private PrintWriter logFileWriter;

//网络爬行者的构造函数

public SearchCrawler(){

//设置应用程序标题栏

setTitle("搜索爬行者");

//设置窗体大小

setSize(600,600);

//处理窗体关闭事件

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

actionExit();

}

});

//设置文件菜单

JMenuBar menuBar=new JMenuBar();

JMenu fileMenu=new JMenu("文件");

fileMenu.setMnemonic(KeyEvent.VK_F);

JMenuItem fileExitMenuItem=new JMenuItem("退出",KeyEvent.VK_X);

fileExitMenuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

actionExit();

}

});

fileMenu.add(fileExitMenuItem);

menuBar.add(fileMenu);

setJMenuBar(menuBar);


网站标题:JAVA网络代码,JAVA 网络
本文路径:http://cdkjz.cn/article/hsepgi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220