tcp/ip java application

P

palmis

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.
 
C

Chris Smith

palmis said:
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?

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top