Solved

Java Server

Posted on 2004-05-01
17
200 Views
Last Modified: 2010-03-31
Ok, I am trying to make a small server for a RPG.  Not really going to be a game, Im just trying to learn about Network Programming and such.  However I wrote a small server and client app.  THey can communicate fine.  However, They only can send string as far as I know.  What If I wanted to send and recieve packets.  

Example:
Client to Server:
1 byte - 0
2 bytes - Length of data
1 byte - Length of  data
String - Username

Something that has somesort of structure and can have like a password and username and stuff in it.  Instead of just sending and recieving one string.

And if I am able to make a client that sends data in a packet form like thus, how do I recieve the packet and take the specific info out of it.

If my question is confusing let me know and Ill try to reword it.

Thanks a lot
0
Comment
Question by:Zeman4323
  • 9
  • 8
17 Comments
 

Author Comment

by:Zeman4323
Comment Utility
o, and how would i get the packets opcode
 thanks
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
you can use PrintWriter to send data...



if your are using Socket...i am assuming so, since u didn't mention abt the type of connection

class X{
    int a,b,c;
    String q,w,e;
    ......
}

PrintWriter p = new PrintWriter(sock.getOutputStream());
X packet = new X(); /*now a small packet */
p.println( packet );
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
class PacketClient{
        byte byte1,length1, length2;
        String username;

       /* other methods necessary */
}

PacketClient packet1 = new PacketClient(); /*a packet */

PrintWriter p = new PrintWriter(sock.getOutputStream());
p.println( packet1 );
0
 

Author Comment

by:Zeman4323
Comment Utility
ok, i am using socket.

However, this will allow me to send packets from the client, but how would I recieve/decode them?
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
you have ObjectInputStream...

ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
PacketClient p = (PacketClient) in.readObject(); /* read a object and convert to your class */
in.close();

if the object recived is not your class type PacketClient, then ClassCastException will be thrown...so to avoid that you can type check it..

Object recv = in.readObject();

if( recv instanceof PacketClass ){
       PacketClass p = (PacketClass) rcvd;
}
else{
   /* some other things */
}
0
 

Author Comment

by:Zeman4323
Comment Utility
Thanks a lot, one last question though

What if all the packets arent the same format, is that possible.  Like maybe one packet has 2 string, and the other only has one.  Should ibe using DataGramPackets?
0
 

Author Comment

by:Zeman4323
Comment Utility
O, and lets say I am trying to make this work with a client I didnt code, may or may not be in java, will it understand a ServerPacket Object?

Because the main thing is recieving packets and getting the information from a client that I ddint code, and then sending packets back that it will understand.  I have the packet documentations of what bytes need to be sent when.  Would this require a completely different server setup?
Sorry, Im quite new to this network programming stuff
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
>>What if all the packets arent the same format, i
no packets cannot be of same format atmost, if any error occurs in the transmission line ( nowadays it's problity is very less ),since each packet or class will have it's unique hashCode or value, so each instance will differ in hashCode

>>> O, and lets say I am trying to make this work with a client I didnt code
 what you mean by "I didn't code", means not your Client what you have created and someones else....if i am right, then if any provison is made in that client to recive your packet, then it will work
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
>>I have the packet documentations of what bytes need to be sent when.  Would this require a completely different server setup?

   you have to code, your own server and client, if it want to use your own format, available client and servers will have their own formats of sending data....since it is yours u have write, since the servers already framed doesn't know that name will corresponds to name of the person or the username u are sending, so u have to specify it....
0
 

Author Comment

by:Zeman4323
Comment Utility
Ok lets start over.  Lets say I want to make an AIM server that current AIM Clients can use.  I have documentation in the form of this:

0 - client to server - Username

1 byte - opcode (0x00)
2 bytes - Length of following data.
string - 'Aim'
1 byte (0x00)
1 byte (0x06)
1 byte (0x00)
2 bytes - client version (0x08 0x0e)
string - '68x\0' (architecture)
1 byte - name length
string - username


0 - server to client - Logon Challenge

1 byte - opcode (0x00)
1 byte (0x00)
1 byte (0x00)
32 bytes - (3d b6 a6 6d 6e 5c 72 66 1e a2 46 2b a2 09 20 34 11 60 1a 9c 9f e7 bd f0 18 16 e8 5b 9a 5b c8 56)-Encryption
byte length1 - (x01)
length1 bytes - (x07)

That Kind of Documentation.  So I know when and what the client will send, and what the server needs to be made to send.  The thing is I dont know how I would set up the server to send something like this:
0 - server to client - Logon Challenge

1 byte - opcode (0x00)
1 byte (0x00)
1 byte (0x00)
32 bytes - (3d b6 a6 6d 6e 5c 72 66 1e a2 46 2b a2 09 20 34 11 60 1a 9c 9f e7 bd f0 18 16 e8 5b 9a 5b c8 56)-Encryption
byte length1 - (x01)
length1 bytes - (x07)

Would I use a DataGram Packet or something?

Sorry,  hope my question makes a tad more sense.  O, and I dont care If I need to rewrite my current server, I just need a short explaination of what needs to be done.  Even if its like check out this section of the Java API.  I am willing to research, I just dont even know where to start.
0
 

Author Comment

by:Zeman4323
Comment Utility
do I just use byte[] somehow to send info as bytes and recieve?
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
>do I just use byte[] somehow to send info as bytes and recieve?
you can
0
 
LVL 7

Accepted Solution

by:
maheshexp earned 400 total points
Comment Utility
>Would I use a DataGram Packet or something?
if u use connectionless , then use it on both side...
u can also put all ur info in a class and send that object through a socket and read it and decode it...
0
 

Author Comment

by:Zeman4323
Comment Utility
Well, I found what I needed.  
Use a ByteBuffer.

then you can putInt
putShort

etc,  and get all of the data out of it you need
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
thanks for the points
why only Grade C??? :-(
0
 

Author Comment

by:Zeman4323
Comment Utility
because I figured it out myself, and did nothing based on what you said
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
>>did nothing based on what you said
     so u didn't find any clue from my words....
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
Increment alphanumeric sequence 6 78
countHi2 challenge 7 43
micro services vs rest web services 16 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now