Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

tcp/ip java application

Thread view: 
palmis - 03 Jan 2006 15:56 GMT
Hi,
I have a problem.
I want to create a tcp/ip java application .
The client is written in C, while my server is written in java. Now I
have found this code to write my server.

//Importo i package necessari
import java.net.*;
import java.io.*;

  public class TCPServer  {
     public void start() throws IOException {
        ServerSocket serverSocket = new ServerSocket(7777);

        //Informazioni sul Server in ascolto
        InetAddress indirizzo = serverSocket.getInetAddress();
        String server = indirizzo.getHostAddress();
        int port = serverSocket.getLocalPort();
        System.out.println("In ascolto Server: "
         + server + " porta: " + port);

        //Ciclo infinito per ascolto dei Client
        while (true) {
            System.out.println("In attesa di chiamate dai Client...
");
            Socket socket = serverSocket.accept();

            //Informazioni sul Client che ha effettuato la chiamata
            InetAddress address = socket.getInetAddress();
            String client = address.getHostName();
            int porta = socket.getPort();
            System.out.println("In chiamata Client: "
             + client + " porta: " + porta);

            //Stream di byte utilizzato per la comunicazione via
socket
            DataInputStream is = new
DataInputStream(socket.getInputStream());
            DataOutputStream os = new
DataOutputStream(socket.getOutputStream());
            while (true) {
                String userInput = is.readLine();
                 if (userInput == null || userInput.equals("QUIT"))
                    break;
                 os.writeBytes(userInput + '\n');
                 System.out.println("Il Client ha scritto: " +
userInput);
              }
            //chiusura della comunicazione con il Client
            os.close();
            is.close();
            System.out.println("Chiusura chiamata Client: "
             + client + "su porta:" + porta);
            socket.close();
          }
     }
     public static void main (String[] args) throws Exception {
           TCPServer tcpServer = new TCPServer();
           tcpServer.start();
     }
  }

What do you think about it?
Now I want know how can I convert stream received from client
(input????) into a byte[]. Have I to use String.getBytes() method?
Thanks.
Chris Smith - 03 Jan 2006 16:35 GMT
> I have a problem.

Yet you didn't tell us what it is...

> I want to create a tcp/ip java application .
> The client is written in C, while my server is written in java. Now I
> have found this code to write my server.

[...]

> What do you think about it?

The first thing I think is that the comments are in a language that I
don't know.

The second is that whoever wrote the code doesn't know anything about
character encodings or internationalization.  It uses two very dangerous
methods -- DataInputStream.readLine, and DataOutputStream.writeBytes --
that have far better alternatives.

> Now I want know how can I convert stream received from client
> (input????) into a byte[]. Have I to use String.getBytes() method?

No, don't use String.getBytes().  Converting bytes to characters and
then back to bytes again is unnecessary, and risks losing information
depending on your platform character encoding.  In short, it is wrong.

How many bytes are you expecting?  Is the protocol byte-counted, or is
there a termination byte to look for?

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.