View Single Post
Old 23-03-2011, 09:57   #1
borexino
Senior Member
 
L'Avatar di borexino
 
Iscritto dal: Feb 2005
Messaggi: 374
[JAVA]Come fare un semplice socket per un client telnet

Ciao a tutti!
di java ne so davvero poco ! Ho questo problema.
devo fare un semplicissimo script in java che apra un semplicissimo socket verso un server mio.server.it sulla porta xxxxx e che una volta collegato si autentichi inviando una stringa cosė composta:

Codice PHP:
"user IZ6SZA pass 11111 vers testsoftware" 
riceva l'output del server ed invii poi una cosa simile:

Codice PHP:
"IZ6SZA-11>APRS:!4321.52N/01217.80E#Test-JAVA_CLIENT" 
Sono riuscito a copiazzare qualcosa ma senza risultati decenti.

Codice PHP:
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class 
Client{
    
Socket socket;
    
DataOutputStream out;
    
DataInputStream in;
     
    
Client(String addressint port){
        try {
            
socket = new Socket("mio.server.it"xxxx);
            
System.err.println("Connesso a "+address+" alla porta "+port);
            
in = new DataInputStream(socket.getInputStream());  
            
out = new DataOutputStream(socket.getOutputStream());
            
out.flush();
            
        } catch (
UnknownHostException e) {
            
e.printStackTrace();
        } catch (
IOException e) {
            
System.err.println("Connessione rifiutata");
        }}
    
    
    
String receive(){
    
String msg="";
    try {
        
msg=(String)in.readLine();
    } catch (
IOException e) {
        
e.printStackTrace();
    }return 
msg;
    }
    
    
void send(String msg)
    {
        try{
            
out.write(2);
            
out.flush();
        }
        catch(
IOException ioException){
            
ioException.printStackTrace();
        }}
    
    
void close(){
        try {
            
in.close();
            
out.close();
            
socket.close();
        } catch (
IOException e) {
            
e.printStackTrace();}}
    
    
    
    
    public static 
void main(String args[]) throws InterruptedException
    
{
        
Client client = new Client("mio.server.it"xxxx);
        
Thread.sleep(2000);
        
client.send("user IZ6... pass 1111 vers testsoftware");
        
Thread.sleep(1000);
        
client.send("IZ6...-11>APRS:!4321.52N/01217.80E#Test-JAVA_CLIENT");
        
Thread.sleep(5000);
                
client.close();
    }

__________________
La ditta PATISCE ma non FALLISCE!!!!
borexino č offline   Rispondi citando il messaggio o parte di esso