1.跪求一个Java编写的聊天聊天多人聊天程序源代码
2.java聊天功能怎么做?
3.怎么:实现两台计算机之间交互式数据通信的源代码。以窗口实现交互的源码 java
跪求一个Java编写的多人聊天程序源代码
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Server
{
public Server()
{
try
{
ServerSocket s=new ServerSocket();
Socket ss=s.accept();
OutputStream out=ss.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
InputStream in=ss.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已经连接到服务器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"
+"你的链接端口:"+ss.getLocalPort()+"\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
//接受客户端信息
class ReadMessage extends Thread
{
private DataInputStream din;
public ReadMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while (true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"客户端说:"+str);
if (str.equals("bye"))
{
System.out.println("客户端下线!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// 发出服务器信息
class SendMessage extends Thread
{
private DataOutputStream dout;
public SendMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
InputStreamReader inr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inr);
String str;
try
{
while(true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("服务器退出!室源开源自动收录网站源码");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Client
{
public Client()
{
try
{
Socket s=new Socket("..1.2",聊天聊天);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("服务器你好!我是源码小刘讲源码 课程客户端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
class ReaderMessage implements Runnable
{
private DataInputStream din;
public ReaderMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while(true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"服务器说:"+str);
if (str.equals("bye"))
{
System.out.println("服务器已经关闭,此程序自动退出!室源视频源码苹果系统");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable
{
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
String str;
InputStreamReader inf=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inf);
try
{
while (true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("客户端自己退出!聊天聊天");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
java聊天功能怎么做?
想要实现java聊天功能比较简单,源码要么直接找源码,室源要么使用第三方的聊天聊天sdk做一些开发。建议可以考虑接入ZEGO即时通讯SDK来实现,源码支持Android java开发,室源集成方便,聊天聊天正64的源码一对一、源码一对多聊天都可快速搭建,室源ipa时间锁源码重要的是不担心消息会丢失,千万级并发也稳定,个人建议你们可以试试。
怎么:实现两台计算机之间交互式数据通信的源代码。以窗口实现交互的 java
一个简单的聊天程序
参考一下;
//客户端的
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc=new ChatClient();
cc.receive();
}
JTextField jtf;
JTextArea jta;
Socket s;
PrintWriter out;
BufferedReader in;
public ChatClient(){
JFrame frame=new JFrame("ChatClient");
frame.setSize(,);
jta=new JTextArea();
jta.setEditable(false);
jtf=new JTextField();
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
s=new Socket(".0.0.1",);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
while(true){
try {
String text=in.readLine();
this.jta.append(text+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send(){
String text=this.jtf.getText();
this.jtf.setText("");
out.println(text);
out.flush();
}
}
//服务器端的
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket();
List list=new ArrayList();
while(true){
Socket s=ss.accept();
list.add(s);
Thread t=new ServerThread(s,list);
t.start();
}
}
}
class ServerThread extends Thread{
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) {
this.s = s;
this.list = list;
try {
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) return;
Iterator it=list.iterator();
while(it.hasNext()){
Socket socket=(Socket)(it.next());
PrintWriter o=new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}