我尝试编写一个关于多客户端 - 服务器套接字编程的大学研究项目的一部分。我的代码工作得很好,所以我给出了验证结果,但问题是我们小组的评估员说我的代码没有很好的速度数据传输连接。如果您在我的代码中发现导致此问题的问题,我将不胜感激。
服务器部分:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import j
ava.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
/**
*
*/
/**
* @author Sina
*
*/
public class BoxServer {
ServerSocket serversocket;
static ThreadHandler t[]=new ThreadHandler[100];
static int size=0;
static ArrayList<Message> messagebox=new ArrayList<Message>();
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(79);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true)
{
try{
//InetAddress inetadress=InetAddress.getLocalHost();
//System.out.println(inetadress);
//System.out.println(inetadress.getHostName());
//System.out.println(inetadress.getHostAddress());
Socket socket=serverSocket.accept();
if(socket==null)
{
System.out.println("null");
}
t[size]=new ThreadHandler(socket,"username");
size++;
t[size-1].start();
}
catch(UnknownHostException e){
System.out.println("salam s");
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println("bye s");
System.out.println(e.getMessage());
}
}
}
}
class ThreadHandler extends Thread{
private String socname;
Socket mySocket;
ObjectInputStream inp;
ObjectOutputStream outp;
public ThreadHandler(Socket s,String socketName)
{
this.mySocket=s;
this.socname=socketName;
}
public void run()
{
try {
inp=new ObjectInputStream(mySocket.getInputStream());
outp=new ObjectOutputStream(mySocket.getOutputStream());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true)
{
System.out.println("thread run");
System.out.println(mySocket.getLocalPort());
System.out.println(mySocket.getLocalAddress());
try {
// System.out.println("my socket:"+mySocket.getOutputStream());
System.out.println(mySocket.isConnected());
System.out.println(inp.available());
System.out.println("inp = "+inp);
System.out.println("reeead "+ inp.readObject());
Message mess=(Message)inp.readObject();
System.out.println("dsd");
System.out.println("mess: "+mess);
BoxServer.messagebox.add(mess);
if(mess.getReceiver().equals("system-use:code=1"))
{
System.out.println(mess.getSender()+" wants to see his/her Inbox");
}
//mySocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("bug dar thread");
e.printStackTrace();
}
}
}
}
客户部分
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.Timer;
public class Main {
/**
* @param args
*/
static Socket socket=new Socket();
public static void main(String[] args) {
System.out.println("newuser(n) or login(l)");
Scanner scanner=new Scanner(System.in);
String typeOfOperation=scanner.nextLine();
if(typeOfOperation.equals("n"))
{
}
else
if(typeOfOperation.equals("l"))
{
try {
socket = new Socket("127.0.0.1",79);
final ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in=new ObjectInputStream(socket.getInputStream());
while(true)
{
Thread timer=new Thread()
{
public void run()
{
while(true)
{
Message temp=new Message();
temp.setReceiver("system-use:code=1");
temp.setSender("username");
try {
out.writeObject(temp);
sleep(5000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
};
timer.start();
String username=scanner.nextLine();
String to=scanner.nextLine();
String body=scanner.nextLine();
Message all=new Message();
all.setText(body);
all.setReceiver(to);
all.setSender(username);
System.out.println("you connected to system");
System.out.println(socket);
System.out.println("now should write");
out.writeObject(all);
System.out.println("ghable threAD");
}
// socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("salaam c");
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("bye c");
System.out.println(e.getMessage());
}
}
else
{
System.out.println("bad operation. try again!");
}
}
}
消息类(我认为只有实体不重要!):
import java.io.Serializable;
public class Message implements Serializable{
String sender;
String receiver;
String text;
boolean delivered=false;
public void delived()
{
this.delivered=true;
}
private String tostringOfClass;
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public void setSender(String sender) {
this.sender = sender;
}
public void setText(String text) {
this.text = text;
}
public String getReceiver() {
return receiver;
}
public String getSender() {
return sender;
}
public String getText() {
return text;
}
public String toString()
{
tostringOfClass="sender : "+sender+" \n"+"receiver : "+receiver+" \n"+"message: "+text;
return tostringOfClass;
}
}