如何用JAVA开发游戏服务器?

如何用JAVA开发游戏服务器?,第1张

首先,我得说明的是,目前市场上新人很难去做pc游戏开发,要么是做页游要么是手游。

页游不清楚我就不说了,手游里面,客户端主要就是c2d和u3d。

如果你想做客户端,那么你可以转学u3d,因为它使用的语言是csharp,这个语言和java相似。网上unity的教程也很多,你随便找一下就有了。

再说说手游服务器,其实我入行服务器还是挺巧合的,当初本来是打算做客户端的,都入职了,因为招不到服务器,就让我转服务器了。。

服务器的教程,我至今也没找到,而且也找不到系统的教程。

开源的服务器框架也只熟悉kbengine和scut。其中一个是cpp做底层python做开发的,一个是csharp做开发。而且scut已经两年没有更新了,应该是死掉了。

所以,想要学服务器开发,得碰点运气。

java服务器目前主流框架技术有网络层netty或mina,数据协议protobuf,数据库mysql,缓存数据库redis,jdbc一般是mybaits或者jpa,项目管理maven,设计层面spring

然后还需要熟悉多线程,linux的基本 *** 作,git或者svn。

差不多了,这些都有个大致的了解,会用,应该就能找到工作了。

后面再深入需要学习的也挺多的,到时候你再根据业务需求自己琢磨着学吧。

import javaawt;
import javaawtevent;
import javaioIOException;
import javanet;
import javaio;
public class chatClient extends Frame {
/
@param args
/
TextField tfTxT=new TextField();
TextArea taContent=new TextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected =false;

public static void main(String[] args) {
new chatClient()lunachFrame();
}

private class RecvThread implements Runnable{
public void run() {
try{
while(bConnected){
String str=disreadUTF();
taContentsetText(taContentgetText()+str+'\n');
}
}catch(IOException e){
eprintStackTrace();
}
}

}

public void lunachFrame(){
thissetLocation(400, 300);
thissetSize(300,300);
//thissetLayout(new FlowLayout());
thisadd(tfTxT,"South");
thisadd(taContent,"North");
pack();
tfTxTaddActionListener(new TFListener());
thisaddWindowListener(new WindowClose());
thissetVisible(true);
connect();
new Thread(new RecvThread())start();
}

public void connect(){
try {
s= new Socket("127001",8888);
dos =new DataOutputStream(sgetOutputStream());
dis =new DataInputStream(sgetInputStream());
Systemoutprintln("connected!");
bConnected=true;
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public void disconnect(){
try {
dosclose();
sclose();
} catch (Exception e) {
// TODO 自动生成 catch 块
eprintStackTrace();
}
}

class WindowClose extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成方法存根
Systemexit(0);
disconnect();
}

}
private class TFListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
String str=tfTxTgetText()trim();//trim去掉两边空格
//taContentsetText(str);
tfTxTsetText("");
try {
doswriteUTF(str);
dosflush();
//dosclose();

} catch (IOException e1) {
e1printStackTrace();

}
}
}
}
======================================
import javaioIOException;
import javanet;
import javaio;
import javautil;
public class ChatServer {
List<Client> clients=new ArrayList<Client>();
Client c=null;
public static void main(String[] args){
new ChatServer()start();
}
public void start(){
boolean started=false;
ServerSocket ss=null;
DataInputStream dis=null;
try{
ss=new ServerSocket(8888);
started =true;
}catch(Exception e)
{
eprintStackTrace();
}
try{
while(started){
Socket s=ssaccept();
c=new Client(s);//启动线程,实行run()方法
Systemoutprintln("a client connected!");
new Thread(c)start();//启动start方法,循环start是Thread中的方法与这上面的start无关
clientsadd(c);
//disclose();
}
} catch (Exception e) {
//eprintStackTrace();
}
finally{
try {
ssclose();
} catch (IOException e) {
// TODO 自动生成 catch 块
eprintStackTrace();
}
}
}
class Client implements Runnable{
private Socket s;
private DataInputStream dis =null;
private boolean bConnected =false;
private DataOutputStream dos=null;
public Client(Socket s){
thiss=s;
try {
dis=new DataInputStream(sgetInputStream());
dos =new DataOutputStream(sgetOutputStream());
bConnected =true;
} catch (IOException e) {
// TODO 自动生成 catch 块
eprintStackTrace();
}
}

public void send(String str)throws Exception{
doswriteUTF(str);

}

public void run() {
try{
while(bConnected){
String str = disreadUTF();
Systemoutprintln(str);
for(int i=0;i<clientssize();i++){
c=clientsget(i);
csend(str);
}
/for(Iterator<Client> it=clientsiterator();ithasNext();){
Client c=itnext();
csend(str);
}/
}
}catch(SocketException e){
clientsremove(this);
Systemoutprintln("客户下线了");
}
catch(EOFException e){
Systemoutprintln("Client closed");
}
catch (Exception e){
//eprintStackTrace();
}
finally{
try {
if(dis !=null) disclose();
if(dos !=null) dosclose();
if(s!=null) sclose();
} catch (Exception e1) {
// TODO 自动生成 catch 块
//e1printStackTrace();
}
}
}
}
}
第一个是客户端,
第二个是server端

服务器端和客户端都是通过SOCKET来进行通信的,首先产生一个
socket实例,通过这个实例,服务器端调用accept这个方法接收来自客户端发送的信息但是在产生socket实例的时候必须初始化一个端口用来负责接受客户端的请求!
客户端要给服务器发送消息也必须产生一个socket实例,初始化的时候必须指定服务器的IP地址,并且指定服务接收的端口号,这样客户端才能找到服务器要接收的地方,找到地方就可以发送过去了。和你写信一样。找到地址
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(socketgetInputStream()));
PrintWriter
out
=
new
PrintWriter(socketgetOutputStream());
BufferedReader
wt
=
new
BufferedReader(new
InputStreamReader(Systemin));
这个只是用来获取一个从键盘的一个流传送给服务器端的数据都是通过流来表示的。意思是是键盘输入的一个字节转化成字符流并输出或者写入!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10289263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-07
下一篇 2023-05-07

发表评论

登录后才能评论

评论列表(0条)

保存