求一个用java socket编写的聊天室程序,能运行的附带源码,要有教程一起发过来吧,邮箱hogsdi112@163.com
小弟java只会点基础,初学阶段,还望各位神神们发的详细一点,怎么使用,怎么编写的,谢谢啦^^能运行就好了,好的话再加分...
小弟java只会点基础,初学阶段,还望各位神神们发的详细一点,怎么使用,怎么编写的,谢谢啦^^
能运行就好了,好的话再加分 展开
能运行就好了,好的话再加分 展开
展开全部
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatRoomServer {
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(8000);
List sockets=new ArrayList();
while(true){
Socket s=ss.accept();
sockets.add(s);
Thread t=new ServerThread1(s,sockets);
t.start();
}
}
}
class ServerThread1 extends Thread{
Socket s;
List sockets;
public ServerThread1(Socket s,List sockets) {
super();
this.s = s;
this.sockets=sockets;
}
public void run(){
try {
BufferedReader in=new BufferedReader(
new InputStreamReader(s.getInputStream()) );
while(true){
String text=in.readLine();
if (text==null) return;
for(int i=0;i<sockets.size();i++){
Socket s2=(Socket)sockets.get(i);
PrintWriter out=new PrintWriter(s2.getOutputStream());
out.println(text);
out.flush();
}
}
} catch (IOException e) {}
finally{
this.sockets.remove(s);
}
}
}
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatRoomClient {
JFrame frame;
JTextField jtf;
JTextArea jta;
Socket s;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) {
ChatRoomClient c=new ChatRoomClient();
c.receive();
}
public ChatRoomClient(){
frame=new JFrame("Chat Room");
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea();
JScrollPane jsp=new JScrollPane(jta);
frame.add(jsp);
jta.setEditable(false);
jtf=new JTextField();
frame.add(jtf,"South");
frame.setVisible(true);
try {
s=new Socket("localhost",8000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text=jtf.getText();
jtf.setText("");
out.println(text);
out.flush();
}
});
}
public void receive(){
try {
while(true){
String text=in.readLine();
jta.append(text+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
先运行ChatRoomServer ,再运行ChatRoomClient
import java.io.*;
import java.util.*;
public class ChatRoomServer {
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(8000);
List sockets=new ArrayList();
while(true){
Socket s=ss.accept();
sockets.add(s);
Thread t=new ServerThread1(s,sockets);
t.start();
}
}
}
class ServerThread1 extends Thread{
Socket s;
List sockets;
public ServerThread1(Socket s,List sockets) {
super();
this.s = s;
this.sockets=sockets;
}
public void run(){
try {
BufferedReader in=new BufferedReader(
new InputStreamReader(s.getInputStream()) );
while(true){
String text=in.readLine();
if (text==null) return;
for(int i=0;i<sockets.size();i++){
Socket s2=(Socket)sockets.get(i);
PrintWriter out=new PrintWriter(s2.getOutputStream());
out.println(text);
out.flush();
}
}
} catch (IOException e) {}
finally{
this.sockets.remove(s);
}
}
}
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatRoomClient {
JFrame frame;
JTextField jtf;
JTextArea jta;
Socket s;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) {
ChatRoomClient c=new ChatRoomClient();
c.receive();
}
public ChatRoomClient(){
frame=new JFrame("Chat Room");
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea();
JScrollPane jsp=new JScrollPane(jta);
frame.add(jsp);
jta.setEditable(false);
jtf=new JTextField();
frame.add(jtf,"South");
frame.setVisible(true);
try {
s=new Socket("localhost",8000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text=jtf.getText();
jtf.setText("");
out.println(text);
out.flush();
}
});
}
public void receive(){
try {
while(true){
String text=in.readLine();
jta.append(text+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
先运行ChatRoomServer ,再运行ChatRoomClient
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询