import javaio;
public class ChatServer
{
final static int thePort=8189;
ServerSocket theServer;
ChatHandler[] chatters;
int numbers=0;
public static void main(String args[])
{
ChatServer app=new ChatServer();
apprun();
}
public ChatServer()
{
try
{
theServer=new ServerSocket(thePort);
chatters=new ChatHandler[10];
}
catch(IOException io){}
}
public void run()
{
try
{
Systemoutprintln("服务器已经建立!");
while(numbers<10)
{
Socket theSocket=theServeraccept();
ChatHandler chatHandler=new ChatHandler(theSocket,this);
chatters[numbers]=chatHandler;
numbers++;
}
}catch(IOException io){}
}
public synchronized void removeConnectionList(ChatHandler c)
{
int index=0;
for(int i=0;i<=numbers-1;i++)
if(chatters[i]==c)index=i;
for(int i=index;i<numbers-1;i++)
chatters[i]=chatters[i+1];
chatters[numbers-1]=null;
numbers--;
}
public synchronized String returnUsernameList()
{
String line="";
for(int i=0;i<=numbers-1;i++)
line=line+chatters[i]user+":";
return line;
}
public void broadcastMessage(String line)
{
Systemoutprintln("发布信息:"+line);
for(int i=0;i<=numbers-1;i++)
chatters[i]sendMessage(line);
}
}====================================================这是客户端:ChatClientjavaimport javaawt;
import javaawtevent;
import javaxswing;
import javanet;
import javaio;
public class ChatClient extends Thread implements ActionListener
{
JTextField messageField,IDField,ipField,portField;
JTextArea message,users;
JButton connect,disconnect;
String user="";
String userList[]=new String[10];
Socket theSocket;
BufferedReader in;
PrintWriter out;
boolean connected=false;
Thread thread;
public static void main(String args[])
{
JFrame frame=new JFrame("聊天室");
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
ChatClient cc=new ChatClient();
JPanel content=cccreateComponents();
framegetContentPane()add(content);
framesetSize(550,310);
framesetVisible(true);
}
public JPanel createComponents()
{
JPanel pane=new JPanel(new BorderLayout());
message=new JTextArea(10,35);
messagesetEditable(false);
JPanel paneMsg=new JPanel();
paneMsgsetBorder(BorderFactorycreateTitledBorder("聊天内容"));
paneMsgadd(message);
users=new JTextArea(10,10);
JPanel listPanel=new JPanel();
listPanelsetBorder(BorderFactorycreateTitledBorder("在线用户:"));
listPaneladd(users);
messageField=new JTextField(50);
IDField=new JTextField(5);
ipField=new JTextField("LocalHost");
portField=new JTextField("8189");
connect=new JButton("连 接");
disconnect=new JButton("断 开");
disconnectsetEnabled(false);
JPanel buttonPanel=new JPanel();
buttonPaneladd(new Label("服务器IP:"));
buttonPaneladd(ipField);
buttonPaneladd(new Label("端口:"));buttonPaneladd(portField);
buttonPaneladd(new Label("用户名:"));
buttonPaneladd(IDField);
buttonPaneladd(connect);
buttonPaneladd(disconnect);
paneadd(messageField,"South");
paneadd(buttonPanel,"North");
paneadd(paneMsg,"Center");
paneadd(listPanel,"West");
connectaddActionListener(this);
disconnectaddActionListener(this);
messageFieldaddActionListener(this);
IDFieldaddActionListener(this);
ipFieldaddActionListener(this);
return pane;
}
public void actionPerformed(ActionEvent e)
{
if(egetSource()==connect){
user=IDFieldgetText();
String ip=ipFieldgetText();
int port =IntegerparseInt(portFieldgetText());
if(!userequals("")&&connectToServer(ip,port,user))
{
disconnectsetEnabled(true);
connectsetEnabled(false);
}
}
if(egetSource()==disconnect)disconnectFromServer();
if(egetSource()==messageField)
if(theSocket!=null)
{
outprintln("MESSAGE:"+messageFieldgetText());
messageFieldsetText("");
}
}
public void disconnectFromServer()
{
if(theSocket!=null)
{
try
{
connected=false;
outprintln("LEAVE:"+user);
disconnectsetEnabled(false);
connectsetEnabled(true);
thread=null;
theSocketclose();
}catch(IOException io){}
theSocket=null;
messagesetText("");
userssetText("");
}
}
public boolean connectToServer(String ip,int port,String ID)
{
if(theSocket!=null)
return false;
try
{
theSocket=new Socket(ip,port);
in=new BufferedReader(new InputStreamReader(theSocketgetInputStream()));
out=new PrintWriter(new OutputStreamWriter(theSocketgetOutputStream()),true);
outprintln("USER:"+user);
messagesetText("");
connected=true;
thread=new Thread(this);
threadstart();
}catch(Exception e){return false;}
return true;
}
public void extractMessage(String line)
{
Systemoutprintln(line);
Message messageline;
messageline=new Message(line);
if(messagelineisValid())
{
if(messagelinegetType()equals("JOIN"))
{
user=messagelinegetBody();
messageappend(user+"进入了聊天室\n");
}
else if(messagelinegetType()equals("LIST"))
updateList(messagelinegetBody());
else if(messagelinegetType()equals("MESSAGE"))
messageappend(messagelinegetBody()+"\n");
else if(messagelinegetType()equals("REMOVE"))
messageappend(messagelinegetBody()+"离开了聊天室\n");
}
else
messageappend("出现问题:"+line+"\n");
}
public void updateList(String line)
{
userssetText("");
String str=line;
for(int i=0;i<10;i++)
userList[i]="";
int index=strindexOf(":");
int a=0;
while(index!=-1){
userList[a]=strsubstring(0,index);
str=strsubstring(index+1);
a++;
index=strindexOf(":");
}
for(int i=0;i<10;i++)
usersappend(userList[i]+"\n");
}
public void run(){
try{
String line="";
while(connected && line!=null){
line=inreadLine();
if(line!=null) extractMessage(line);
}
}catch(IOException e){}
}
} =======================================================import javanet;
import javaio;
class ChatHandler extends Thread{
Socket theSocket;
BufferedReader in;
PrintWriter out;
int thePort;
ChatServer parent;
String user="";
boolean disconnect=false;
public ChatHandler(Socket socket,ChatServer parent){
try{
theSocket=socket;
thisparent=parent;
in=new BufferedReader(new InputStreamReader(theSocketgetInputStream()));
out=new PrintWriter(new OutputStreamWriter(theSocketgetOutputStream()),true);
thePort=theSocketgetPort();
start();
}catch(IOException io){}
}
public void sendMessage(String line){
outprintln(line);
}
public void setupUserName(String setname){
user=setname;
//Systemoutprint(user+"参加");
parentbroadcastMessage("JOIN:"+user);
}
public void extractMessage(String line){
Message messageline;
messageline = new Message(line);
if(messagelineisValid()){
if(messagelinegetType()equals("USER")){
setupUserName(messagelinegetBody());
parentbroadcastMessage("LIST:"+parentreturnUsernameList());
}
else if(messagelinegetType()equals("MESSAGE")){
parentbroadcastMessage("MESSAGE:"+user+"说: "+messagelinegetBody());
}
else if(messagelinegetType()equals("LEAVE")){
String c=disconnectClient();
parentbroadcastMessage("REMOVE:"+c);
parentbroadcastMessage("LIST:"+parentreturnUsernameList());
}
}
else
sendMessage("命令不存在!");
}
public String disconnectClient(){
try{
inclose();
outclose();
theSocketclose();
parentremoveConnectionList(this);
disconnect=true;
}catch(Exception ex){}
return user;
}
public void run(){
String line,name;
boolean valid=false;
try{
while((line=inreadLine())!=null){
Systemoutprintln("收到:"+line);
extractMessage(line);
}
}catch(IOException io){}
}
}
=========================================================
Messagejavapublic class Message{
private String type;
private String body;
private boolean valid;
public Message(String messageLine){
valid=false;
type=body=null;
int pos=messageLineindexOf(":");
if(pos>=0){
type=messageLinesubstring(0,pos)toUpperCase();
body=messageLinesubstring(pos+1);
valid=true;
}
}
public Message(String type,String body){
valid=true;
thistype=type;
thisbody=body;
}
public String getType(){
return type;
}
public String getBody(){
return body;
}
public boolean isValid(){
return valid;
}
} ==================================================共有4个文件,先运行服务段端。。。 这是我以前学的时候写过的!希望能帮的上你
一、服务器配置
对于所有服务器来说,配置越高,价格也就越高。比如一台普通独立游戏服务器配置的价格月付在1000元人民币以内,中等配置的价格月付在1000到5000块钱之间,高端配置的价格月付一般在万元以上。不同配置的价格相差很大,其性能相差也很大。因此,大家要合理的进行选择,选择适合自己的即可。
二、服务器带宽
对于游戏服务器来说,带宽是器最重要的一个因素。带宽的大小与质量的高低均是影响游戏服务器的重要原因。百兆共享带宽与百兆独享带宽的价格相差非常大,至于具体的价格也都不太一样,当然,也跟机房有一定的关系。
三、安全防御性能
对于游戏服务器来说,一般都很容易受到黑客的攻击,特别是对于一些热门网络游戏。所以,选择一家能提供高防服务器的IDC商尤为重要。对于游戏服务器来说,防御能力越强,其价格也就越高。
逃离塔科夫有七个俄区服务器。七个服务器分别是叶卡特琳堡、阿斯塔纳、莫斯科1、莫斯科2、圣彼得堡、莫斯科3、哈巴洛夫斯克。在俄区服务器前面都有三个字母RUS,你只要点击任意带有RUS的服务器就能进俄区玩儿了。
值得一提的是,因为逃离塔科夫是俄罗斯游戏,所有俄区玩家比较多,服务器也会比较卡。其中哈巴洛夫斯克的延迟更是接近500左右。所有进俄区之前一定要开加速,以尽量避免卡顿。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)