今天道尔智控就给我们广大朋友来聊聊javatcp门禁,以下7个关于javatcp实例的观点希望能帮助到您找到想要的答案。
java环境下tcp协议教程
本文贡献者:【瓦素小怪兽┑ ̄Д ̄┍】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案package chatClient;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;
import DownLoad.*;
import config.*;
public class ChatClient extends JFrame {
private static Logger log=Logger.getLogger(ChatClient.class);
Socket s = null;
TextField tf = new TextField();
TextArea ta = new TextArea(null,11,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
Panel p=new Panel(new GridLayout(2,6,2,3));
Label l3=new Label("下裁的文件名:");
TextField tf1 = new TextField();
Button b3=new Button("下载");
Label l4=new Label("上传的文件名:");
TextField tf2 = new TextField();
Button b4=new Button("上传");
Label l1=new Label("用户名:");
Label l2=new Label("密码:");
TextField name = new TextField();
JPasswordField pwd=new JPasswordField();
Button b1=new Button("连接");
Button b2=new Button("断开");
SimpleDateFormat st=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DataOutputStream dos = null;
DataInputStream dis = null;
String cname;
boolean success=false;
boolean start=true;
public static void main(String[] args) {
new ChatClient().launchFrame();
log.debug("test");
log.info("info");
log.error("error");
}
public void launchFrame(){
setLocation(300,333);
p.add(l1);
p.add(name);
p.add(l2);
p.add(pwd);
p.add(b1);
p.add(b2);
p.add(l3);p.add(tf1);p.add(b3);p.add(l4);p.add(tf2);p.add(b4);
ta.setCaretPosition(ta.getText().length());
add(p,BorderLayout.NORTH);
add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.CENTER);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
if(!success){disconnect();}
System.exit(0);
}
});
tf.addActionListener(new TFListener());
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(check()){
connect();
if(!success){
new Thread(new Receve()).start();
}
}else{
JOptionPane.showMessageDialog(null, "用户名或密码错误,请确认后重输!");
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
disconnect();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tf1.getText().equals("")){
JOptionPane.showMessageDialog(null, "输入要下裁的文件名!");
}else{
new DownloadStartupC().start(tf1.getText());
}
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tf2.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入要上传的文件名和路径!");
}else{
new DownloadStartup().start(tf2.getText());
}
}
});
setVisible(true);
this.setResizable(false);
}
public boolean check(){
cname=name.getText();
String pwd1=pwd.getText();
String pwd2=FileUtil.getMemberData(cname);
if(pwd1.equals(pwd2)){
return true;
}else{
return false;
}
}
public void connect(){
start=true;
try {
s= new Socket (FileUtil.getServerInfoData("serverhost"),Integer.parseInt(FileUtil.getServerInfoData("serverport")));
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
dos.writeUTF(cname+"登录 "+st.format(new Date()));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "服务器端异常,请稍后重试!");
success=true;
return;
}
success=false;
b1.setEnabled(false);
}
public void disconnect (){
try {
if(dos!=null){
dos.writeUTF(cname+"退出 "+st.format(new Date()));
dos.close();}
if(s!=null){
s.close();}
} catch (IOException e) {
e.printStackTrace();
}
success=true;
b1.setEnabled(true);
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
tf.setText("");
try {
dos.writeUTF(cname+":"+str);
dos.flush();
} catch (Exception e1) {
log.info(e.toString());
JOptionPane.showMessageDialog(null, "未连接,请连接后再发言!");
}
}
}
private class Receve implements Runnable{
public void run() {
try{
while(start){
String s= dis.readUTF();
//if(s.equals("服务器已关闭,请重新登录!")){
// start=false;
// b1.setEnabled(true);
// }
ta.setText(ta.getText()+s+'\n');
ta.setCaretPosition(ta.getText().length());
}
} catch(IOException e){
start=false;
b1.setEnabled(true);
// e.printStackTrace();
}
}
}
}
package ChatServer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import DownLoad.DownloadStartup;
import config.FileUtil;
public class ChatServer extends Frame {
ServerSocket ss = null;
Socket s = null;
boolean start = false;
DataInputStream dis = null;
List clients = new ArrayList();
TextArea ta = new TextArea(null,11,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
JPanel p = new JPanel(new GridLayout(2, 2, 5, 5));
TextField tf = new TextField();
JButton b3 = new JButton("上传");
JButton b1 = new JButton("start");
JButton b2 = new JButton("stop");
class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean stat = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
stat = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (Exception e) {
clients.remove(this);
// e.printStackTrace();
}
}
public void run() {
Client c = null;
try {
while (stat) {
String str = dis.readUTF();
ta.setText(ta.getText()+str+"\n");
ta.setCaretPosition(ta.getText().length());
for (int i = 0; i < clients.size(); i++) {
c = (Client) clients.get(i);
c.send(str);
}
// System.out.println(clients.size());
}
} catch (Exception e) {
//System.out.println("closed!");
} finally {
try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new ChatServer().launchFrame();
}
public void launchFrame() {
setLocation(300, 333);
this.setSize(300, 300);
this.setTitle("服务器端");
p.add(b1);
p.add(b2);
p.add(tf);
p.add(b3);
ta.setCaretPosition(ta.getText().length());
add(p, BorderLayout.SOUTH);
add(ta, BorderLayout.CENTER);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Thread t=new Thread(new DisConnection());
t.start();
Client c = null;
for (int i = 0; i < clients.size(); i++) {
c = (Client) clients.get(i);
c.send("服务器已关闭,请稍后重新登录!");
}
System.exit(0);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setTitle("服务器启动");
Thread t=new Thread(new Connection());
t.start();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setTitle("服务器关闭");
Thread t=new Thread(new DisConnection());
t.start();
Client c = null;
for (int i = 0; i < clients.size(); i++) {
c = (Client) clients.get(i);
c.send("服务器已关闭,请稍后重新登录!");
}
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println(tf.getText());
if(tf.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入要上传的文件名和路径!");
}else{
new DownloadStartup().start(tf.getText());}
}
});
setVisible(true);
}
private class Connection implements Runnable{
public void run() {
try {
ss = new ServerSocket(Integer.parseInt(FileUtil.getServerInfoData("serverport")));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "服务器启动不成功,重试!");
}
try {
start = true;
while (start) {
s = ss.accept();
Client c = new Client(s);
// System.out.println("A Client connected!");
new Thread(c).start();
clients.add(c);
}
} catch (Exception e) {
System.out.println("服务器关闭");
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class DisConnection implements Runnable{
public void run() {
try {
if(s!=null){
s.close();
}
if(ss!=null){
ss.close();
System.out.println("close");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream out=new FileOutputStream("Chathistory/history.txt");
out.write(ta.getText().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
给你个例子 自己看吧
两个类 可以运行的 你运行起来看看
上文就是道尔智控小编解疑贡献者:(瓦素小怪兽┑ ̄Д ̄┍)解答的关于“java环境下tcp协议教程”的问题了,不知是否已经解决你的问题?如果没有,下一篇内容可能是你想要的答案,下面继续讲析下文用户【安稳善良】分析的“java实现tcp和udp网络通信”的一些相关疑点做出分析与解答,如果能找到你的答案,可以关注本站。

java实现tcp和udp网络通信
本文贡献者:【安稳善良】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案你这种说法本身就有待商榷。Java是一种商用开发语言,封装了很多上层的类完成企业开发需要。目的就是在企业开发中可以快速精准的完成企业开发要求。例如,ServerSocket/Socket帮助你完成tcp协议,DatagramPacket帮助你完成UDP协议。你却需要自己实现协议。而Java语言本身对于这些底层操作系统相关的调用都比较麻烦。所以,如果你真要了解通信本身的底层协议,建议你用C语言来完成,而不是Java。
建议你的老师也好好了解一下各种语言的使用范围。在国外Java是不建议在大学的计算机课程中讲的,因为这会让你很难了解操作系统及其整个计算机系统,只能快速完成某些具体开发任务。建议对于计算机科学和计算机体系机构方面的科研教学,使用比较底层的语言,例如C语言。
以上就是道尔智控小编解答(安稳善良)解答关于“java实现tcp和udp网络通信”的答案,接下来继续为你详解用户(桃花无数)回答“tcp udp区别优缺点”的一些相关解答,希望能解决你的问题!
tcp udp区别优缺点
本文贡献者:【桃花无数】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案java中用类代替了C语言的结构体 java.io包里有个相应的类,可以把任何类写到文件中去,相关的有 ObjectOutputStream 和 FileOutputStream,自己去看吧
以上就是道尔智控小编解答贡献者:(桃花无数)解答的关于“tcp udp区别优缺点”的问题了,不知是否已经解决你的问题?如果没有,下一篇内容可能是你想要的答案,接下来继续讲述下文用户【情忘寂】分享的“javatcp报文大小”的一些相关疑点做出分析与解答,如果能找到你的答案,可以关注本站。
javatcp报文大小
本文贡献者:【情忘寂】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案tcp报文,首先你们要设计好包头和包尾,比如0x2是包头,0x3就是包尾。这样收发解析才不会乱。
里面的协议基本上就是字节的形式就好。无非就是服务端encode一下,客户端decode。
不过你在做这个之前一定要写好测试,否则数字节能数死人。
以上就是道尔智控小编解答(情忘寂)贡献关于“javatcp报文大小”的答案,接下来继续为你详解用户(红尘几度欢颜笑)解答“Java TCP socket通信,如何实现发送十六进制值,并在数据接收窗口中显示这些数据对应的字符串,非常感谢!”的一些相关解答,希望能解决你的问题!
Java TCP socket通信,如何实现发送十六进制值,并在数据接收窗口中显示这些数据对应的字符串,非常感谢!
本文贡献者:【红尘几度欢颜笑】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案我自己的电脑上有一段源代码,就是基于TCP聊天小代码,能进行相互之间的消息接受。我的代码是直接传输字符串的,不是16进制滴。嗯,也贴出来看看吧!
运行服务器,c1,c2就可以了,c1与c2可进行通信。
Client.java
import java.net.*;
public class Client{
static byte num=1;
private int portNum;
public Client(int portnum){
this.portNum=portnum;
System.out.println("您是第"+num+"位客服端");
num++;
}
public void sentMessage(String me){
//都是向服务器发信息端口号1999
try{
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp=new DatagramPacket(me.getBytes(),me.length(),InetAddress.getByName("127.0.0.1"),1999);
ds.send(dp);
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
public String receiveMessage(){
String str="";
try{
DatagramSocket ds=new DatagramSocket(this.portNum);//表示哦自己的接收端口号是1999
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
str=new String(dp.getData(),0,dp.getLength());
ds.close();
}catch(Exception e){
e.printStackTrace();
}
return str;
}
}
c1.java
import java.util.*;
public class c1 implements Runnable{
Client cl;
boolean goon=true;
Scanner sc=new Scanner(System.in);
public c1(){
cl=new Client(2000);
System.out.println("这里是2000客户端\n你可以发送信息。请输入要发送的信息。out退出");
new Thread(this).start();
while(this.goon==true){
say();
}
if(goon==false){
System.exit(0);
}
}
public static void main(String[] args){
new c1();
}
public void say(){
String mess=sc.nextLine();
System.out.println("是否发送y/n");
String key=sc.nextLine();
if(key.equals("y")){
System.out.println("信息信息发送中……");
try{
cl.sentMessage(mess);
}catch(Exception e){
e.printStackTrace();
}
}
else if(key.equals("out")){
goon=false;
}
}
public void run(){
while(this.goon==true){
String sst="";
try{
sst=cl.receiveMessage();
}catch(Exception e){
e.printStackTrace();
}
if(sst.length()>0){
System.out.println(sst);
}
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println("程序结束!");
}
}
c2.java
import java.util.*;
public class c2 implements Runnable{
Client cl;
boolean goon=true;
Scanner sc=new Scanner(System.in);
public c2(){
cl=new Client(2001);
System.out.println("这里是2001客户端\n你可以发送信息。请输入要发送的信息。out退出");
new Thread(this).start();
while(goon==true){
say();
}
if(goon==false){
System.exit(0);
}
}
public static void main(String[] args){
new c2();
}
public void say(){
String mess=sc.nextLine();
System.out.println("是否发送y/n");
String key=sc.nextLine();
if(key.equals("y")){
System.out.println("信息信息发送中……");
try{
cl.sentMessage(mess);
}catch(Exception e){
e.printStackTrace();
}
}
else if(key.equals("out")){
this.goon=false;
}
}
public void run(){
while(this.goon==true){
String sst="";
try{
sst=cl.receiveMessage();
}catch(Exception e){
e.printStackTrace();
}
if(sst.length()>0){
System.out.println(sst);
}
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println("聊天关闭!");
}
}
Server.java
import java.net.*;
import java.util.*;
public class Server implements Runnable{
private String message;
boolean work=true;
byte tm=10;
String[] clomessage={"信息:正在断开网络连接.",".",".\n","信息:设置保存中……","","","完成\n","信息:欢迎下次使用\n","信息:完成\n","Goodbye!"};
public Server(){
new Thread(this).start();
System.out.println("本程序为服务端Server");
Scanner sc=new Scanner(System.in);
System.out.println("输入命令out关闭服务器");
String clo=sc.nextLine();
if(clo.equals("out")){
System.out.println("正在关闭服务器……");
setwork(false);
System.exit(0);
}
}
public static void main(String[] args){
new Server();
}
public void setwork(boolean bo)
{
this.work=bo;
}
public void setMessage(String str){
this.message=str;
}
public String getMessage(){
return this.message;
}
public void sentMessage(){
String mes=this.getMessage();
try{
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp=new DatagramPacket(mes.getBytes(),mes.length(),InetAddress.getByName("127.0.0.1"),2000);
DatagramPacket dp2=new DatagramPacket(mes.getBytes(),mes.length(),InetAddress.getByName("127.0.0.1"),2001);
ds.send(dp);
ds.send(dp2);
ds.close();
System.out.println("信息发送至:127.0.0.1:2000 & 127.0.0.1:2001");
this.setMessage("");
}catch(Exception e){
e.printStackTrace();
}
}
public void receiveMessage() throws Exception{
try{
DatagramSocket ds=new DatagramSocket(1999);//表示哦自己的接收端口号是1999
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
if(str.length()>0){
this.setMessage(str);
System.out.println("信息:Server从"+dp.getAddress().getHostAddress()+"主机(端口号:"+dp.getPort()+")收到信息:"+this.getMessage());
}
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
while(tm>0){
if(work==false){
tm--;
System.out.print(clomessage[9-tm]);
}
try{
receiveMessage();//时刻接受信息
}catch(Exception e){
e.printStackTrace();
}
if(this.getMessage().length()>0){//如果接收到信息则发送信息
try{
sentMessage();
}catch(Exception e){
e.printStackTrace();
}
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
呵呵,请指教啊!
以上就是道尔智控小编解答(红尘几度欢颜笑)分析关于“Java TCP socket通信,如何实现发送十六进制值,并在数据接收窗口中显示这些数据对应的字符串,非常感谢!”的答案,接下来继续为你详解用户(魔恋双仙)解答“java tcp发送报文”的一些相关解答,希望能解决你的问题!
java tcp发送报文
本文贡献者:【魔恋双仙】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案你可以用socket编程,java socket是封装了TCP协议的,不需要你去设置里面的一些参数了。
你还可以用java jpcap编写,这个可以发送接收arp,tcp,udp,icmp等各种报文
以上就是道尔智控小编分享贡献者:(魔恋双仙)分析的关于“java tcp发送报文”的问题了,不知是否已经解决你的问题?如果没有,下一篇内容可能是你想要的答案,下面继续:下文用户【∥。农村妇女】回答的“java怎么写tcp服务端”的一些相关疑点做出分析与解答,如果能找到你的答案,可以关注本站。
java怎么写tcp服务端
本文贡献者:【∥。农村妇女】, 疑问关键字:javatcp门禁, 下面就让道尔智控小编为你解答,希望本文能找到您要的答案!
最佳答案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();
}
}
本文关于[javatcp门禁]的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。