📄 server.java
字号:
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.Timer.*;
import javax.swing.*;
import java.awt.*;
class Server extends JFrame implements ActionListener
{
JFrame jframe;
JButton start,stop;
JPanel p;
public Server()
{
jframe=new JFrame("Chat Server");
p=new JPanel();
jframe.getContentPane().add(p);
start=new JButton("start");
stop=new JButton("stop");
p.add(start);
p.add(stop);
start.addActionListener(this);
stop.addActionListener(this);
Color color=new Color(90,114,146);
p.setBackground(color);
jframe.setSize(300,300);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent ent)
{
Object b=ent.getSource();
if(b==start)
{
new AppServer();
}
else if(b==stop)
{
jframe.dispose();
}
}
public static void main(String []args)
{
Server s=new Server();
}
}
// Code for the AppSever class
class AppServer implements Runnable
{
ServerSocket server;
Socket fromClient;
Thread serverThread;
public AppServer()
{
System.out.println("Server start>>>>>");
try
{
server=new ServerSocket(4833);
serverThread=new Thread(this);
serverThread.start();
}
catch(Exception e)
{
System.out.println("cann't start the thread "+e);
}
}
public void run()
{
try
{
while(true)
{
fromClient=server.accept();
Connect con=new Connect(fromClient);
}
}
catch(Exception e)
{
System.out.println("cannt listen to the client");
}
}
}
//code for the connect class
class Connect
{
ObjectOutputStream streamToClient;
int ctr=0;
BufferedReader streamFromClient;
static Vector vector;
static Vector vlist;
String message=" ";
static String str=new String("UesrList");
static
{
vector=new Vector(1,1);
vlist=new Vector(1,1);
vlist.addElement((String)str);
}
int verify(String mesg)
{
try
{
RandomAccessFile f=new RandomAccessFile("UP.txt","r");
int i=0;
String str="";
while((f.getFilePointer())!=(f.length()))
{
str=f.readLine();
if(str.equals(mesg))
{
ctr=1;
break;
}
}
f.close();
}
catch(Exception e)
{
System.out.println("Exception occurred: "+e);
}
return ctr;
}
int checkFile(String mesg)
{
int chk=1;
try
{
RandomAccessFile RS=new RandomAccessFile("UP.txt","r");
int i=0;
String str="";
String colon=new String(":");
int index=((String)mesg).lastIndexOf(colon);
String userName=(String)mesg.substring(0,index);
while((RS.getFilePointer())!=(RS.length()))
{
str=RS.readLine();
String colon1=new String(":");
int index1=((String)str).lastIndexOf(colon1);
String uName=(String)str.substring(0,index1);
if(uName.equals(userName))
{
chk=0;
break;
}
}
RS.close();
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
return chk;
}
public Connect(Socket inFromClient)
{
String msg="";
String mesg="";
try
{
streamFromClient=new BufferedReader(new InputStreamReader(inFromClient.getInputStream()));
streamToClient=new ObjectOutputStream(inFromClient.getOutputStream());
msg=streamFromClient.readLine();
if((msg.equals("From Timer")))
{
streamToClient.writeObject(vector);
streamToClient.writeObject(vlist);
}
else if(msg.equals("LoginInfo"))
{
msg=streamFromClient.readLine();
int ver=verify(msg);
if(ver==1)
{
String colon=new String(":");
int index=((String)msg).lastIndexOf(colon);
String userName=(String)msg.substring(0,index);
if(!(vlist.indexOf((String)userName)>0))
{
streamToClient.writeObject("Welcome");
vlist.addElement((String)userName);
}
}
else
{
streamToClient.writeObject("Login denied");
}
}
else if(msg.equals("RegisterInfo"))
{
msg=streamFromClient.readLine();
int ret=checkFile(msg);
if(ret==0)
streamToClient.writeObject("User Exists");
if(ret==1)
{
FileOutputStream out=new FileOutputStream("UP.txt",true);
PrintStream p=new PrintStream(out);
p.println(msg);
p.close();
streamToClient.writeObject("Registered");
}
}
else if(msg.equals("User Logout"))
{
String remUser=streamFromClient.readLine();
vlist.removeElement((String)remUser);
}
else
{
message=message+msg;
vector.addElement((String)message);
streamToClient.writeObject(vector);
}
}
catch(Exception e)
{
System.out.println("cannot get client stream connect"+e);
}
finally
{
try
{
inFromClient.close();
}
catch(IOException e)
{
System.out.println("OH,no "+e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -