
正文
java远程视频聊天代码 java远程视频聊天代码怎么用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
急需一个java编程实现的简单聊天窗口代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false); //文本域只读
JScrollPane sp=new JScrollPane(ta); //滚动窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("发送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("AA:"+message+"\n"); //添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30); //文本域,第一个参数为行数,第二个参数为列数
ta.setEditable(false); //文本域只读
JScrollPane sp=new JScrollPane(ta); //滚动窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("发送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket server=null;
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
server=new ServerSocket(5000);
//ta.append("等待AA连接...\n");
socket=server.accept();
//ta.append("AA已连接\n");
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread1 mt=new MyThread1(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos));
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText(); //获取文本框中的内容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败!");
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("AA:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
相关问答
Q1: 怎么用JAVA实现QQ的视频聊天
网络编程,比较复杂点
服务器ServerSocket ss=new ServerSocker();
Socket s=ss.accept();
客户端Socket s=new Socket;
输入输出流
客户端发信息到服务器,服务器将信息转发到需要接受的客户端
需要考虑线程问题,并发问题。
Q2: 救助:Java可以编写一个语音视频聊天程序吗?
我开始学JAVA的时候也想做之类的程序,但是搜了好久,也在百度知道里问过,就是没人回答。估计不行。建议就别做了。要做语音视频程序,还不如用C#或C++呢。JAVA本来就不适合做桌面应用程序了,学j2ee或j2me吧。
Q3: java能开发视频聊天吗
肯定的说:能!
大致的说一下原理:
首先你要学习一下java的网络编程方面的东西,像TCP/IP UDP协议等等的东西,因为要编写视频聊天程序,这些理论性的东西是必须的。
现在假设你已经可以编写出简单功能的网络聊天功能的软件了,想在就是要用你编写软件驱动起你的摄像头了。首先你要确保你的摄像头的开发商已经给了你该摄像头的驱动接口,有了这个接口你就可以编写出可以驱动起该摄像头的java程序了。
之后你要学习一下JNI,也就是Java Native Interface,学会这个,你就可以用你编写的java程序来调用摄像头驱动程序(驱动一般都是用C或C++)编写,有了JNI,你就可以让你的java程序和驱动的C或C++程序来进行通讯了。
之后把摄像头的实时摄像信息传到网络的另一边(用到网络编程),就可以让对方看到你了(前提是对方也安装了你编写的软件,否则你们之间的通讯可能没人能看懂)。
这是个大致的过程,实现的过程肯定会晕倒这样那样的问题,不过不要担心,学习的过程就是遇到问题,思考问题,解决问题的过程。这样慢慢的你就发现你已经很牛了!
java远程视频聊天代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java远程视频聊天代码怎么用、java远程视频聊天代码的信息别忘了在本站进行查找喔。






