Добрый день, начал писать чат и столкнулся с такой проблемой - клиент подключается к серверу, сервер принимает сообщение и отправляет его обратно клиенту, НО , при подключении еще одного клиента сервер посылает ему только его сообщения, ВОПРОС: как сделать так, чтоб сервер посылал сообщение всем подключенным к нему клиентам? заранее спасибо и если можно, поделитесь примером smile Ниже коды сервера и клиента: Сервер: Код | public class Server { // public static void main(String args[]) { System.out.println("* Socket Server *"); ServerSocket ss = null; try { ss = new ServerSocket(8183); // ServerSocket serverSocket = new ServerSocket(8189); } catch(Exception ex) { System.out.println(ex.toString()); System.exit(0); } int nPort = ss.getLocalPort(); System.out.println( "Local Port: " + nPort); ServerThread sThread = null; sThread = new ServerThread(ss); sThread.start(); System.out.println( "Waiting connection..."); try { sThread.join(); } catch(InterruptedException ex) { System.out.println(ex.toString()); } try { ss.close(); } catch(Exception ex) { System.out.println(ex.toString()); } System.exit(0); } }
//============================================ //Class ServerThread //============================================ class ServerThread extends Thread { ServerSocket ss = null; Socket s = null;
// ============================================ // ServerThread // ============================================ public ServerThread(ServerSocket sSocket) { ss = sSocket; }
// ============================================ // run // ============================================ public void run() { InputStream is; OutputStream os; try { s = ss.accept(); } catch(Exception ex) { stop(); } System.out.println("Connected."); try { while (true){ System.out.println("444444444*"); InputStream socketIn = s.getInputStream();
int read; String resi = recvString(socketIn);
System.out.println(resi);
os = s.getOutputStream(); MyClass my = new MyClass(); sendString(os, resi); os.flush(); os.close(); socketIn.close(); } } catch(Exception ex) { System.err.println(ex.toString()); stop(); } try { s.close(); } catch(Exception ex) { stop(); } }
// ============================================ // sendString // ============================================ static void sendString (OutputStream os, String s) throws IOException { System.out.println(s); for(int i = 0; i < s.length(); i++) { os.write((byte)s.charAt(i)); } os.write('\n'); os.flush(); }
// ============================================ // recvString // ============================================ static String recvString(InputStream is) throws IOException { String szBuf = ""; int ch = is.read();
while (ch >= 0 && ch != '\n') { szBuf += (char)ch; ch = is.read(); } return szBuf; } }
|
Клиент: Код | import java.io.FileInputStream; import java.io.*; import java.net.Socket;
import javax.swing.*;
public class SoccetClass{ Socket socket; form f; OutputStream socketOut; FileInputStream fis = null; FileOutputStream out; public SoccetClass(){
try { //new form(); socket = new Socket("localhost", 8183); socketOut = socket.getOutputStream();
// socket.close(); } catch(IOException ex) { ex.printStackTrace(); } finally { try { if(fis != null) fis.close(); } catch(IOException ex) { ex.printStackTrace(); } } } public void set(String s, JTextArea jTextArea1) { try { String str = jTextArea1.getText(); InputStream is; String sr ="jk"; sendString(socketOut, s);
is = socket.getInputStream(); String szStr; szStr = recvString(is); jTextArea1.setText(szStr);
} catch(IOException ex) { ex.printStackTrace(); } }
static void sendString (OutputStream os, String s) throws IOException { System.out.println(s); for(int i = 0; i < s.length(); i++) { os.write((byte)s.charAt(i)); } os.write('\n'); os.flush(); } static String recvString(InputStream is) throws IOException { String szBuf = "";
System.out.println("22"); //String szBuf = ""; int ch = is.read(); System.out.println(ch); while (ch >= 0 && ch != '\n') { szBuf += (char)ch; ch = is.read(); }
return szBuf; } static public String getKbdString() { byte bKbd[] = new byte[256]; int iCnt = 0; String szStr = ""; try { iCnt = System.in.read(bKbd); } catch(Exception ex) { System.out.println(ex.toString()); } szStr = new String(bKbd, 0, iCnt); szStr = szStr.trim(); return szStr; } }
|
Это сообщение отредактировал(а) FreakOnALeash - 9.4.2008, 13:34
|