聊天的java代碼,java編寫聊天軟件

速求用JAVA語言寫聊天室的源代碼

【ClientSocketDemo.java 客戶端Java源代碼】

襄州網站建設公司創新互聯,襄州網站設計制作,有大型網站制作公司豐富經驗。已為襄州上千家提供企業網站建設服務。企業網站搭建\外貿網站建設要多少錢,請找那個售后服務好的襄州做網站的公司定做!

import java.net.*;

import java.io.*;

public class ClientSocketDemo

{

//聲明客戶端Socket對象socket

Socket socket = null;

//聲明客戶器端數據輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明字符串數組對象response,用于存儲從服務器接收到的信息

String response[];

//執行過程中,沒有參數時的構造方法,本地服務器在本地,取默認端口10745

public ClientSocketDemo()

{

try

{

//創建客戶端socket,服務器地址取本地,端口號為10745

socket = new Socket("localhost",10745);

//創建客戶端數據輸入輸出流,用于對服務器端發送或接收數據

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

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

//獲取客戶端地址及端口號

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

//向服務器發送數據

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

//從服務器接收數據

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執行過程中,有一個參數時的構造方法,參數指定服務器地址,取默認端口10745

public ClientSocketDemo(String hostname)

{

try

{

//創建客戶端socket,hostname參數指定服務器地址,端口號為10745

socket = new Socket(hostname,10745);

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

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

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執行過程中,有兩個個參數時的構造方法,第一個參數hostname指定服務器地址

//第一個參數serverPort指定服務器端口號

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort));

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

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

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

String comd[] = args;

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port");

ClientSocketDemo demo = new ClientSocketDemo();

}

else if(comd.length == 1)

{

System.out.println("Use default port");

ClientSocketDemo demo = new ClientSocketDemo(args[0]);

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user");

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

}

else System.out.println("ERROR");

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服務器端Java源代碼】

import java.net.*;

import java.io.*;

public class ServerSocketDemo

{

//聲明ServerSocket類對象

ServerSocket serverSocket;

//聲明并初始化服務器端監聽端口號常量

public static final int PORT = 10745;

//聲明服務器端數據輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明InetAddress類對象ip,用于獲取服務器地址及端口號等信息

InetAddress ip = null;

//聲明字符串數組對象request,用于存儲從客戶端發送來的信息

String request[];

public ServerSocketDemo()

{

request = new String[3]; //初始化字符串數組

try

{

//獲取本地服務器地址信息

ip = InetAddress.getLocalHost();

//以PORT為服務端口號,創建serverSocket對象以監聽該端口上的連接

serverSocket = new ServerSocket(PORT);

//創建Socket類的對象socket,用于保存連接到服務器的客戶端socket對象

Socket socket = serverSocket.accept();

System.out.println("This is server:"+String.valueOf(ip)+PORT);

//創建服務器端數據輸入輸出流,用于對客戶端接收或發送數據

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

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

//接收客戶端發送來的數據信息,并顯示

request[0] = in.readUTF();

request[1] = in.readUTF();

request[2] = in.readUTF();

System.out.println("Received messages form client is:");

System.out.println(request[0]);

System.out.println(request[1]);

System.out.println(request[2]);

//向客戶端發送數據

out.writeUTF("Hello client!");

out.writeUTF("Your ip is:"+request[1]);

out.writeUTF("Your port is:"+request[2]);

}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo();

}

}

高手進,java實現聊天功能?

。。。這個我以前也遇到過、不過我是用C#寫的、不過后來也有java的、你可以、使用Swing做的簡單界面,及使用Socket套接字實現簡單聊天 。。。。。。但是、我不知道你問的是C/S模式還是B/S 模式?

其中、B/S模式可以用Servlet來實現,思路是通過Context上下文綁定參數實現

而C/S模式的,是通過RMI遠程調用的方法實現的。。。先給你個C/S模式的核心代碼。。。import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.net.*;public class Server extends JFrame

{

public static void main(String [] args)

{

Server server=new Server();

//設定框架的關閉方式

server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//顯示框架

server.setVisible(true);

server.pack();

CreateServer cs=new CreateServer(server);

}

// 設定框架的寬度和高度

private static final int WIDTH=450;

private static final int HEIGHT=450;

// 聊天信息框

JTextArea mainArea=new JTextArea(12,35);

// 發送信息的填寫框

JTextArea sendArea=new JTextArea(5,30);

// 構造函數

public Server()

{

//定位框架

Toolkit kit=Toolkit.getDefaultToolkit();

Dimension screenSize=kit.getScreenSize();//獲取電腦當前分辨率

int width=screenSize.width;

int height=screenSize.height;

int x=(width-WIDTH)/2;

int y=(height-HEIGHT)/2;

//設置窗口顯示位置

setLocation(x,y);

//設置框架大小

setSize(WIDTH,HEIGHT);

//設置標題

setTitle("小新新聊天服務器");

//設置窗口的自定義大小

setResizable(false);

//在內容表格上創建3個面板并加入到內容表格

Container con=this.getContentPane();

JPanel labelPanel=new LabelPanel();

con.add(labelPanel,BorderLayout.NORTH);

JPanel contentPanel=new ContentPanel();

con.add(contentPanel,BorderLayout.CENTER);

JPanel sendPanel=new SendPanel();

con.add(sendPanel,BorderLayout.SOUTH);

}

//聊天窗口的標題面板

class LabelPanel extends JPanel

{

public LabelPanel()

{

Font font=new Font("Dialog",Font.BOLD,18);

JLabel label=new JLabel("歡迎使用小新新聊天服務器");

label.setFont(font);

this.add(label);

}

}

// 聊天信息查看面板

//該面板內的區域為不可編輯區域

class ContentPanel extends JPanel

{

public ContentPanel()

{

FlowLayout fl=new FlowLayout(FlowLayout.CENTER);

this.setLayout(fl);

mainArea.setLineWrap(true);

mainArea.setEditable(false);

JScrollPane scrollPanel=new JScrollPane(mainArea);

this.add(scrollPanel);

}

}

// 填寫發送信息的面板

class SendPanel extends JPanel

{

public SendPanel()

{

//面板的組件之間水平分隔15像素,垂直間距10像素

FlowLayout layout=new FlowLayout(FlowLayout.LEFT,15,10);

this.setLayout(layout);

sendArea.setLineWrap(true);

JScrollPane scrollPanel=new JScrollPane(sendArea);

this.add(scrollPanel);

JButton send=new JButton("發送");

this.add(send);

//對發送按鈕注冊動作監聽器

send.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String msg=sendArea.getText();

if(!msg.equals(""))

{

mainArea.append("【服務器】:"+msg+"\n");

sendArea.setText("");

CreateServer.sendMsg(msg);

}

else

{

return;

}

}

});

}

}

}

//創建服務器ServerSocket的類

class CreateServer extends Thread

{

private Server server;

private static BufferedReader in=null;//存儲客戶端發送到服務器的數據

private static PrintWriter out=null;//存儲服務器發送到客戶端的數據

private Socket socket=null;//等待客戶端連接socket

private ServerSocket ss=null;//開啟服務器socket連接

//構造函數

public CreateServer(Server s)

{

this.server=s;

try

{

ss=new ServerSocket(2345);

System.out.println("服務器成功啟動...!");

socket=ss.accept();//等待客戶端請求

//獲取輸入到服務器的數據

in=new BufferedReader(new InputStreamReader(socket.getInputStream()));

//獲取輸出到客戶端的數據

out=new PrintWriter(socket.getOutputStream(),true);

out.println("你好!");

}

catch(Exception r)

{

r.printStackTrace();

}

this.start();//啟動線程

}

//實現信息發送到客戶端的發送方法

public static void sendMsg(String s)

{

try

{

out.println("【服務器】:"+s+"\n");

}

catch(Exception e)

{

System.out.println("發送信息失敗...!!");

e.printStackTrace();

}

}

// 線程Thread類的run方法實現對客戶端發送來的數據監聽

//線程啟動后開始該方法,執行線程體

public void run()

{

String msg="";

while(true)

{

try

{

msg=in.readLine();

//Thread.sleep(500);//線程睡眠

}

catch (SocketException ex)

{

ex.printStackTrace();

break;

}

catch(IOException r)

{

r.printStackTrace();

break;

}

//若從客戶端獲取的信息不為空對象也不為空串

//則把信息顯示在聊天信息文本域

if(msg!=null msg.trim()!="")

{

server.mainArea.append(msg+"\n");

}

}

}

}

java 聊天室代碼???

前天幫人解決過,代碼如下:public class iChat extends JFrame {

private JPanel jContentPane;

private JButton jButton1;

private JScrollPane jScrollPane1;

private JTextArea jTextArea1,jTextArea2; public iChat() {

super();

initialize();

} private void initialize() {

this.setSize(375, 364);

jTextArea1 = getJTextArea1();

this.setContentPane(getJContentPane());

this.setTitle("聊天室");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setBackground(Color.BLUE);//藍色為JPanel 。

jContentPane.setLayout(null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJTextArea2(), null);

jContentPane.add(getJScrollPane1(), null);

}

return jContentPane;

} private JTextArea getJTextArea2() {

if (jTextArea2 == null) {

jTextArea2 = new JTextArea();

jTextArea2.setBounds(new Rectangle(1, 253, 282, 73));

jTextArea2.setLineWrap(true);

jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyTyped(java.awt.event.KeyEvent e) {

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

String s = "你說:" + jTextArea2.getText();

String date = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss").format(Calendar

.getInstance().getTime());

jTextArea1.append(date + "\n");

jTextArea1.append(s);

}

}

});

}

return jTextArea2;

} private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setBounds(new Rectangle(293, 288, 62, 32));

jButton1.setText("發送");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

String s = "你說:" + jTextArea2.getText();

String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

.format(Calendar.getInstance().getTime());

jTextArea1.append(date + "\n");

jTextArea1.append(s + "\n");

jTextArea2.setText(""); }

});

}

return jButton1;

}

private JScrollPane getJScrollPane1() {

if (jScrollPane1 == null) {

jScrollPane1 = new JScrollPane();

jScrollPane1.setBounds(new Rectangle(0, 0, 360, 220));

jScrollPane1.setViewportView(getJTextArea1());

}

return jScrollPane1;

} private JTextArea getJTextArea1() {

if (jTextArea1 == null) {

jTextArea1 = new JTextArea();

jTextArea1.setEditable(false);

jTextArea1.setLineWrap(true);

}

return jTextArea1;

} public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

iChat thisClass = new iChat();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}}

用JAVA 編寫簡單網絡聊天程序

/**

* 基于UDP協議的聊天程序

*

* 2007.9.18

* */

//導入包

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.net.*;

public class Chat extends JFrame implements ActionListener

{

//廣播地址或者對方的地址

public static final String sendIP = "172.18.8.255";

//發送端口9527

public static final int sendPort = 9527;

JPanel p = new JPanel();

List lst = new List(); //消息顯示

JTextField txtIP = new JTextField(18); //填寫IP地址

JTextField txtMSG = new JTextField(20); //填寫發送消息

JLabel lblIP = new JLabel("IP地址:");

JLabel lblMSG = new JLabel("消息:");

JButton btnSend = new JButton("發送");

byte [] buf;

//定義DatagramSocket的對象必須進行異常處理

//發送和接收數據報包的套接字

DatagramSocket ds = null;

//=============構造函數=====================

public Chat()

{

CreateInterFace();

//注冊消息框監聽器

txtMSG.addActionListener(this);

btnSend.addActionListener(this);

try

{

//端口:9527

ds =new DatagramSocket(sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

//============接受消息============

//匿名類

new Thread(new Runnable()

{

public void run()

{

byte buf[] = new byte[1024];

//表示接受數據報包

while(true)

{

try

{

DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);

ds.receive(dp);

lst.add("【消息來自】◆" + dp.getAddress().getHostAddress() + "◆"+"【說】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);

}

catch(Exception e)

{

if(ds.isClosed())

{

e.printStackTrace();

}

}

}

}

}).start();

//關閉窗體事件

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent w)

{

System.out.println("test");

int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);

if(n==JOptionPane.YES_OPTION)

{

dispose();

System.exit(0);

ds.close();//關閉ds對象//關閉數據報套接字

}

}

});

}

//界面設計布局

public void CreateInterFace()

{

this.add(lst,BorderLayout.CENTER);

this.add(p,BorderLayout.SOUTH);

p.add(lblIP);

p.add(txtIP);

p.add(lblMSG);

p.add(txtMSG);

p.add(btnSend);

txtIP.setText(sendIP);

//背景顏色

lst.setBackground(Color.yellow);

//JAVA默認風格

this.setUndecorated(true);

this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

this.setSize(600,500);

this.setTitle("〓聊天室〓");

this.setResizable(false);//不能改變窗體大小

this.setLocationRelativeTo(null);//窗體居中

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

this.setVisible(true);

txtMSG.requestFocus();//消息框得到焦點

}

//===============================Main函數===============================

public static void main(String[]args)

{

new Chat();

}

//================================發送消息===============================

//消息框回車發送消息事件

public void actionPerformed(ActionEvent e)

{

//得到文本內容

buf = txtMSG.getText().getBytes();

//判斷消息框是否為空

if (txtMSG.getText().length()==0)

{

JOptionPane.showMessageDialog(null,"發送消息不能為空","提示",JOptionPane.WARNING_MESSAGE);

}

else{

try

{

InetAddress address = InetAddress.getByName(sendIP);

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

ds.send(dp);

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

txtMSG.setText("");//清空消息框

//點發送按鈕發送消息事件

if(e.getSource()==btnSend)

{

buf = txtMSG.getText().getBytes();

try

{

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

txtMSG.setText("");//清空消息框

txtMSG.requestFocus();

}

}

}

急求一個JAVA編寫的局域網聊天代碼?不要求很復雜。只要可以在兩臺機子上簡單的對話就可以。

//以下引入包

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.applet.*;

public class regit extends JApplet implements ActionListener,ItemListener//接口

{

String s=null;

//對象的聲明

JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//標簽

JTextField textname,textaddress,textmail,textphone;//文本框

JRadioButton r1,r2;//單選按紐

ButtonGroup bg;//組

JPasswordField textpass;//密碼域

JButton buttonregit,buttonreset;//注冊按紐 重寫按紐

JPanel p;//面板

String sex;//定義性別字符串

URL url;//統一資源定位

BufferedWriter out1,out2;//流

BufferedReader in;

//布局方式

GridBagLayout gbl;

GridBagConstraints gc;

AppletContext co;//接口

//初始化

public void init()

{

//new 對象

labelname=new JLabel("用 戶 名:");

labelpass=new JLabel("用戶密碼:");

labelsxe=new JLabel("性別:");

labeladdress=new JLabel("地址:");

labelmail=new JLabel("電子郵件:");

labelphone=new JLabel("聯系電話:");

textname=new JTextField(15); textname.setForeground(Color.red);

textname.setToolTipText("請在這輸入你的用戶名");

textaddress=new JTextField(15);textaddress.setForeground(Color.red);

textaddress.setToolTipText("請在這輸入你的地址");

textmail=new JTextField(15); textmail.setForeground(Color.red);

textmail.setToolTipText("請在這里輸入你的E-mail地址");

textphone=new JTextField(15); textphone.setForeground(Color.red);

textphone.setToolTipText("請在這輸入你的電話號碼");

r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//設置顏色

r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//設置顏色

bg=new ButtonGroup();

bg.add(r1);bg.add(r2);//加入組,實現單選

textpass=new JPasswordField(15);

textpass.setToolTipText("在這里輸入密碼");

textpass.setForeground(Color.red);

buttonregit=new JButton("注冊"); buttonregit.setBackground(new Color(47,177,210));//設置顏色

buttonregit.setToolTipText("點擊按紐完成注冊");

buttonreset=new JButton("填寫"); buttonreset.setBackground(new Color(47,177,210));//設置顏色

buttonreset.setToolTipText("點擊按紐刷新重寫");

gbl=new GridBagLayout(); ///////////////////////////////////////

gc=new GridBagConstraints(); //////采用GridBagLayout布局方式////////

p=new JPanel();

p.setLayout(gbl);

p.setBackground(new Color(47,177,210));

this.getContentPane().add(p);//加入面板

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=2;

gbl.setConstraints(labelname,gc);

p.add(labelname);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=2;

gbl.setConstraints(textname,gc);

p.add(textname);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=4;

gbl.setConstraints(labelpass,gc);

p.add(labelpass);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=4;

gbl.setConstraints(textpass,gc);

p.add(textpass);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=6;

gbl.setConstraints(labelsxe,gc);

p.add(labelsxe);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=6;

gbl.setConstraints(r1,gc);

p.add(r1);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=5;

gc.gridy=6;

gbl.setConstraints(r2,gc);

p.add(r2);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=8;

gbl.setConstraints(labeladdress,gc);

p.add(labeladdress);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=8;

gbl.setConstraints(textaddress,gc);

p.add(textaddress);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=10;

gbl.setConstraints(labelmail,gc);

p.add(labelmail);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=10;

gbl.setConstraints(textmail,gc);

p.add(textmail);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=12;

gbl.setConstraints(labelphone,gc);

p.add(labelphone);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=12;

gbl.setConstraints(textphone,gc);

p.add(textphone);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=16;

gbl.setConstraints(buttonregit,gc);

p.add(buttonregit);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=16;

gbl.setConstraints(buttonreset,gc);

p.add(buttonreset);

/////////////////////////////////////////////

co=this.getAppletContext();

/////////////////////////////////////////////

buttonregit.addActionListener(this);//按紐事件的監聽

buttonreset.addActionListener(this);//按紐事件的監聽

r1.addItemListener(this);//選擇事件的監聽

r2.addItemListener(this);//選擇事件的監聽

textphone.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

//注冊按紐事件

if(e.getSource()==buttonregit)

{

String s1=textname.getText(); ////////////////////

String s2=new String(textpass.getPassword());////////////////////

String s3=textaddress.getText(); // 定義字符串 //

String s4=textmail.getText(); ////////////////////

String s5=textphone.getText(); ////////////////////

//判斷注冊資料 信息 是否為空

if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);

return;//彈出對話框并返回

}

try//寫入到txt文件

{

in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));

}

catch(Exception ee){}

String ss=s1;

try

{

while((s=in.readLine())!=null)

{

if(s.startsWith(ss))

{

JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");

textname.setText("");//設置為空,重新輸入

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

return;

}

}

}

catch(Exception ee){}

///////////////////////////以上代碼判斷是否有同名

{

try

{

out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));

out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));

}//創建文件

catch(Exception ee)

{}

try

{

out1.write(s1+"#"+s2);//寫

out1.newLine();

out2.write("用戶名:"+s1);

out2.newLine();

out2.write("密碼:"+s2);

out2.newLine();

out2.write("性別:"+sex);

out2.newLine();

out2.write("地址:"+s3);

out2.newLine();

out2.write("電子郵件:"+s4);

out2.newLine();

out2.write("電話:"+s5);

out2.newLine();

out1.flush();

out2.flush();//清理緩沖

out1.close();

out2.close();

}

catch(Exception ee)

{}

JOptionPane.showMessageDialog(null,"注冊成功!");

try

{

String qss="";

url=new url(/qss);//連接上網址

co.showDocument(url);

}

catch(Exception exx)

{}

}

}

//////////////////////以下為回車事件

if(e.getSource()==textphone)

{

String s1=textname.getText(); ////////////////////

String s2=new String(textpass.getPassword());////////////////////

String s3=textaddress.getText(); // 定義字符串 //

String s4=textmail.getText(); ////////////////////

String s5=textphone.getText(); ////////////////////

//判斷注冊資料 信息 是否為空

if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);

return;//彈出對話框并返回

}

try//寫入到txt文件

{

in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));

}

catch(Exception ee){}

String ss=s1;

try

{

while((s=in.readLine())!=null)

{

if(s.startsWith(ss))

{

JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");

textname.setText("");//設置為空,重新輸入

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

return;

}

}

}

catch(Exception ee){}

///////////////////////////以上代碼判斷是否有同名

{

try

{

out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));

out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));

}//創建文件

catch(Exception ee)

{}

try

{

out1.write(s1+"#"+s2);//寫

out1.newLine();

out2.write("用戶名:"+s1+"密碼:"+s2+"性別:"+sex+"地址:"+s3+"電子郵件:"+s4+"電話:"+s5);//寫

out2.newLine();

out1.flush();

out2.flush();//清理緩沖

out1.close();

out2.close();

}

catch(Exception ee)

{}

JOptionPane.showMessageDialog(null,"注冊成功!");

try

{

String qss="";

url=new url(/qss);//連接上網址

co.showDocument(url);

}

catch(Exception exx)

{}

}

}

if(e.getSource()==buttonreset)//刷新重寫事件

{

textname.setText("");

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

}

}

//////////////////////////////////////////

//

public void itemStateChanged(ItemEvent ex)

{

if(ex.getSource()==r1)

{

sex=new String("男");

}

else if(ex.getSource()==r2)

{

sex=new String("女");

}

}

}

參考資料:試試看,剛在網上找的

要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(8888);

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("192.168.1.2",8888);

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編寫聊天軟件
URL地址:http://m.kartarina.com/article12/hsdegc.html

成都網站建設公司_創新互聯,為您提供電子商務網站收錄網頁設計公司ChatGPT營銷型網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都app開發公司
主站蜘蛛池模板: 免费播放美女一级毛片 | 色综合久久无码中文字幕| 国产真人无码作爱免费视频| 欧洲精品无码成人久久久| 在线观看免费无码视频| 国产v亚洲v天堂无码网站| 97精品人妻系列无码人妻| 中文字幕无码av激情不卡 | 免费无码专区毛片高潮喷水| 自拍中文精品无码| 狠狠精品干练久久久无码中文字幕| 日韩精品无码一区二区三区AV| 亚洲日韩国产精品无码av| 一本一道中文字幕无码东京热| 91精品无码久久久久久五月天| 人妻少妇无码精品视频区| 国产精品无码亚洲一区二区三区 | 中文字幕日韩精品无码内射| 亚洲精品无码av片| 亚洲日韩乱码中文无码蜜桃臀 | 中文字幕无码成人免费视频| 亚洲av无码片在线播放| 亚洲成A∨人片天堂网无码| 久久精品无码中文字幕| 亚洲av永久中文无码精品| 亚洲av无码一区二区三区网站| 国产a v无码专区亚洲av| 亚洲国产精品无码久久青草| 国产福利无码一区在线 | 无码一区18禁3D| 无码av人妻一区二区三区四区| 久久精品九九热无码免贵| 亚洲中文字幕无码日韩| 亚洲综合无码精品一区二区三区| 免费无码av片在线观看| 自拍中文精品无码| 国产精品亚韩精品无码a在线 | 国产AV无码专区亚洲A∨毛片| 中文成人无码精品久久久不卡 | 亚洲成?v人片天堂网无码| 亚洲午夜无码片在线观看影院猛|