javasocket編程聊天
⑴ 求一個用java socket編寫的聊天室程序,能運行的附帶源碼,有客戶端和伺服器端
也不知道怎麼說怎麼用,我寫的代碼,很久了,用的是awt,感覺Java在應用程序上沒前景所以就沒在深入了……現在主攻J2ee,代碼給你,你自己感覺吧
服務端:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("埠使用中....");
System.out.println("請關掉相關程序並重新運行伺服器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客戶端,開兩個就能聊了……
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("�˳��ˣ�bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
⑵ JAVA socket聊天室程序 readLine()阻塞
對每一個客戶端都建立一個線程來接收消息,發消息與接消息不要在同一線程上,那就才能解決阻塞問題。一般新手用socket編程,大都是遇到阻塞問題不懂解決,導致了收發消息失敗。其實網上socket聊天通訊的例子大把,遇到問題查查看,思考下就能明白了。
有問題歡迎提問,滿意請採納,thx.
⑶ 急!!!!求JAVA 帝!JAVA socket 編程,實現聊天程序或其他功能
//這是一個批量文件傳輸程序,先運行server,然後運行client。即可將client端的e:\\1文件內的所有內容傳輸給server端,並按照原來的文件夾結構放在server端電腦的e:\\2文件夾內
/*
* MyClient.java
*
* Created on 2009年8月16日, 下午1:37
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
public class MyClient
{
Socket client;
boolean flag=true;
FileInputStream fis;//此輸入流負責讀取本機上要傳輸的文件
DataOutputStream dos;//此輸出流負責向另一台電腦(伺服器端)傳輸數據
DataInputStream dis;//此輸入流負責讀取另一台電腦的回應信息
public static void main(String[] args)
{
new MyClient().ClientStart();
}
public void ClientStart()
{
try
{
client=new Socket("192.168.2.101",30000);//伺服器端的IP,(這個只是在區域網內的)我的是這個,你的根據實際而定
//client=new Socket("localhost",30000);
System.out.println("已連接");
dos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
transmit(new File("e:\\1\\"));
String s="/]00";//提示傳輸完畢的標記
byte b[]=s.getBytes();
dos.write(b,0,s.length());
dos.flush();
}
catch(IOException e)
{
System.out.println("Error");
}
}
public void transmit(File f)throws IOException//這是傳輸的核心,而且將被遞歸
{
byte b[];
String ts;
int ti;
for(File f1:f.listFiles())
{ //首先通過if語句判斷f1是文件還是文件夾
if(f1.isDirectory()) //fi是文件夾,則向伺服器端傳送一條信息
{
ts="/]0f"+(f1.getPath().replace("e:\\1\\",""));//"/]0f"用於表示這條信息的內容是文件夾名稱
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
transmit(f1);//由於f1是文件夾(即目錄),所以它裡面很有可能還有文件或者文件夾,所以進行遞歸
}
else
{
fis=new FileInputStream(f1);
ts="/]0c"+(f1.getPath().replace("e:\\1\\",""));//同上,表示這是一個文件的名稱
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
dos.writeInt(fis.available());//傳輸一個整型值,指明將要傳輸的文件的大小
dos.flush();
dis.read();
b=new byte[10000];
while(fis.available()>0)//開始傳送文件
{
ti=fis.read(b);
dos.write(b,0,ti);
dos.flush();
}
dos.flush();
fis.close();
dis.read();
}
}
}
}
/*
* MyServer.java
*
* Created on 2009年8月16日, 上午11:44
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import java.io.*;
import java.net.*;
public class MyServer
{
ServerSocket server=null;
Socket client=null;
boolean flag=true;
DataInputStream dis;
DataOutputStream dos;
FileOutputStream fos;
public static void main(String[] args)
{
new MyServer().ServerStart();
}
public void ServerStart()
{
try
{
server=new ServerSocket(30000);
System.out.println("埠號:"+server.getLocalPort());
client=server.accept();
System.out.println("連接完畢");
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
String answer="g";
byte ans[]=answer.getBytes();
byte b[]=new byte[1000];
int ti;
new File("e:\\2").mkdirs();
while(flag)
{
ti=dis.read(b);
dos.write(ans);
String select=new String(b,0,ti);
if(select.contains("/]0f"))
{
File f=new File("e:\\2\\"+(select.replace("/]0f","")));
System.out.println("creat directory");
f.mkdirs();
}
else if(select.contains("/]0c"))
{
fos=new FileOutputStream("e:\\2\\"+(select.replace("/]0c","")));
String cs;
boolean cflag=true;
int tip=dis.readInt();
dos.write(ans);
while(tip>0)
{
ti=dis.read(b,0,(tip>1000?1000:tip));
tip=tip-ti;
cs=new String(b,0,4);
fos.write(b,0,ti);
}
fos.flush();
fos.close();
dos.write(ans);
}
else if(select.contains("/]00"))
{
flag=false;
}
}
dis.close();
client.close();
server.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
}
⑷ java socket聊天原理是怎樣的
一般做法是客戶端不斷去輪詢伺服器:「有沒有最新的消息?」,輪詢頻率高一些,給人的感覺就是實時通訊了。
還有就是客戶端也有監聽,伺服器發現有新的消息,及向保持聯系的客戶端發送消息。
⑸ 如何用java tcp socket實現聊天室
一個埠就夠了。底層實現就是socket的鏈接。每次server端accept一個鏈接就會創建一個新的socket用於會話。你可以創建一個類room,包含兩個ws,當鏈接之後,把ws填入到room中。滿了就可以開始聊天室。然後server繼續等待新的ws
⑹ 基於java socket編程實現多用戶的聊天問題
你只實現了單工聊天功能, 並沒有實現雙工聊天功能。 也就是說,伺服器端 沒有實現 客戶端A和客戶端B的通信 。 你會mv模式吧, 在model功能層里開一個線程 並用 s.accept()接受從客戶端 發來的套接字,然後轉給另一個客戶端,如果這條路打通了 你就可以 實現雙工通信, 不是很難的 ,細心點 試試看。