socket编程是我们杨老师给我们最后一个上机实验,由于没有涉及到数据库,可能它的效率就没有那么高,就是简单的将线程和cs模式运用到一块,所以。写的有问题的地方还希望大神指出来:
话不多说,直接上代码:
服务端
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SingleTalkServer {
public void handle() throws IOException{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
int clientNumber = 0;
try {
while(true){
clientSocket = serverSocket.accept(); //程序将在此等候客户端的连接
clientNumber++; //记录客户数目
new MultiTalkServerThread(serverSocket,clientSocket, clientNumber).start(); //创建一个新线程处理此客户请
}
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println("Accept OK!");
}
public static void main(String[] args )throws IOException{
new SingleTalkServer().handle();
} }
class MultiTalkServerThread extends Thread implements ActionListener
{
private Frame f;
private Button sending,receiveing;
private TextField send;
private TextArea receive;
private PrintWriter out;
private BufferedReader in;
private String sinputLine, inputLine;
private String record;
boolean sinbye,inbye;
private Socket socket;
ServerSocket server;
private int clientNumber;
public MultiTalkServerThread(ServerSocket server,Socket socket, int clientNumber)
{
this.server = server;
this.socket = socket;
this.clientNumber = clientNumber;
}
public void run()
{
try {
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} //auto flush
sinbye = false;
inbye = false;
f=new Frame("qq server boss Test");
f.setSize(280,250);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
send=new TextField(80);
receive=new TextArea(30,80);
f.add(send);
f.add(receive);
sending = new Button("发送");
receiveing= new Button("关闭");
f.add(sending);
f.add(receiveing);
sending.addActionListener(this);
receiveing.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setBackground(Color.lightGray);
f.setVisible(true);
record="";
try {
inputLine = in.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(inputLine.equals("Bye."))
inbye=true;
record=record+"from Client: "+inputLine+"\n";
receive.setText(record);
if(inbye){
sinbye=true;
out.println("Bye.");
out.flush();
}
else
send.setText("Server input:");
while( true )
{
if( inbye == false )
{
try {
inputLine = in.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
record=record+"from Client: "+inputLine+"\n";
receive.setText(record);
if (inputLine.equals("Bye."))
inbye = true;
}
if( sinbye == false )
{
if(inbye){
out.println("Bye.");
out.flush();
sinbye=true;
}
else
send.setText("Server input:");
}
if( sinbye == true && inbye == true )
break;
}
out.close();
try {
in.close();
socket.close();
server.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==sending){
if( sinbye == false )
{
sinputLine=send.getText();
sinputLine=sinputLine.replace("Server input:","");
out.println(sinputLine);
out.flush();
if (sinputLine.equals("Bye."))
sinbye = true;
}
}
else if(e.getSource()==receiveing){
System.exit(0);
}
}
}
客户端
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SingleTalkClient implements ActionListener
{
private Frame f;
private Button sending,receiveing;
private TextField send;
private TextArea receive;
private PrintWriter out;
private BufferedReader in;
private BufferedReader stdIn;
private String fromServer, fromUser;
private String record;
boolean sbye,ubye;
public void actionPerformed(ActionEvent e){
if(e.getSource()==sending){
if(ubye==false){
fromUser=send.getText();
fromUser=fromUser.replace("Client input:","");
out.println(fromUser);
out.flush();
if (fromUser.equals("Bye."))
ubye = true;
}
}
else if(e.getSource()==receiveing){
System.exit(0);
}
}
public void handle() throws IOException {
Socket client = null;
out = null;
in = null;
try {
client = new Socket("127.0.0.1", 4444);
out = new PrintWriter(client.getOutputStream(), true); //auto flush
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: 127.0.0.1.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: 127.0.0.1.");
System.exit(1);
}
//创建图形界面
f=new Frame("qq clinet boss Test");
f.setSize(280,250);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
send=new TextField(80);
receive=new TextArea(30,80);
f.add(send);
f.add(receive);
sending = new Button("发送");
receiveing= new Button("关闭");
f.add(sending);
f.add(receiveing);
sending.addActionListener(this);
receiveing.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setBackground(Color.lightGray);
f.setVisible(true);
record="";
send.setText("Client input:");
sbye = false;
ubye = false;
while( true )
{
if( sbye == false )
{
fromServer = in.readLine();
record=record+"from Server: "+fromServer+"\n";
receive.setText(record);
if (fromServer.equals("Bye."))
sbye = true;
}
if( ubye == false )
{
if(sbye){
out.println("Bye.");
out.flush();
ubye=true;
}
else
send.setText("Client input:");
}
if( ubye == true && sbye == true )
break;
}
out.close();
in.close();
client.close();
}
public static void main(String[] args) throws IOException {
new SingleTalkClient().handle();
}
}
怎么说呢,这个如果在客户端加一个线程就不仅仅实现一对多的想法,就可以真正意义实现“群聊”的想法;但是由于没有数据库,那么真正意义群聊还是差点火候,但是模式是对的。学习不能只学习,还得思考。