Solved

Java Server

Posted on 2004-05-01
17
201 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
ID: 10969778
o, and how would i get the packets opcode
 thanks
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10969863
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
ID: 10969865
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
ID: 10969883
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
ID: 10969910
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
ID: 10969920
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
ID: 10969932
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
ID: 10969956
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:maheshexp
ID: 10969961
>>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
ID: 10971727
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
ID: 10971828
do I just use byte[] somehow to send info as bytes and recieve?
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971901
>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
ID: 10971923
>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
ID: 11010844
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
ID: 11011346
thanks for the points
why only Grade C??? :-(
0
 

Author Comment

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

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image decoding from Camera 3 73
javap bin 2 30
even odd program using while loop 3 31
expectj telnet failing 5 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

861 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

25 Experts available now in Live!

Get 1:1 Help Now