Socket

Status
Nicht offen für weitere Antworten.

Kaladial

Bekanntes Mitglied
hi

ich hab folgenden Code:

Code:
sock = new Socket(ip, port); //Hier die Domain eintragen
out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());

nun möchte ich wissen wie ich den timeout verringern kann wenn bei
sock = new Socket(ip, port);

nix dran hängt....

weis das jemand?
thx für Hilfe
Kala
 
bringt nix ...

sock = new Socket(ip, port);

in der zeile kommt ja schon der fehler bzw braucht er ja schon solange wenn auf der anderen seite kein rechner ist...
und sowas in der art funktioniert ja net:

sock = new Socket(ip, port).setSoTimeout(20);
 
hmmm hatte ich auch überlegt ...
ich überleg nur wie ich dann den socket aufbau versuch unterbrechen soll ...

also wenn ich sag

geh in thread 1
-> mach in dem thread ne socket auf


geh in thread 2
-> warte 1 sec
-> guck ob thread 1 fertig is
-> nein -> stoppe thread 1

^^ so ungefähr hät ich mir das jetzt gedacht nur wird damit ja der versuch zu connecten net unterbrochen , da der thread ja trotzdem bis zum ende durchläuft und dann halt nur net neu beginnt oder?

also was ich mir überlegt hab is nen ping auf den zielhost zu schicken und wenn ne antwort kommt dann versuch ich verbindung aufzubauen... nur überlg ich grad ob es sowas wie nen ping befehl unter java gibt oder ob ich wieder das windows ping einbauen muss
 
> nur wird damit ja der versuch zu connecten net unterbrochen , da der thread ja trotzdem bis zum ende durchläuft und dann halt nur net neu beginnt oder?

welcher Thread? hilft sock.close() nicht?


da du wieder den Anschein gibst, in einer Welt ohne Suchmaschinen zu leben, hier noch zwei Links
http://www.coding-board.de/board/archive/index.php/t-4251.html

http://www.velocityreviews.com/forums/t146589-ping-class-java.html

so richtig helfen kann ich aber nicht bei der Sache und nur für dich zu suchen ist ja auch nicht hilfreich 😉
viel Erfolg noch
 
Code:
package AVS3;

import java.io.*;
import java.net.*;

public class Ping2 {
	public static long testConn(byte[] addr1, int port, int timeoutMs){
		//pass in a byte array with the ipv4 address, the port & the max time out required
		long start = -1; //default check value
		long end = -1; //default check value
		long total = -1; // default for bad connection
	
		//make an unbound socket
		Socket theSock = null;
		try {
			InetAddress addr = InetAddress.getByAddress(addr1);
System.out.println(addr);			
			SocketAddress sockaddr = new InetSocketAddress(addr,port);
		
			//Create the socket with a timeout
			//when a timeout occurs, we will get timout exp.
			//also time our connection this gets very close to the real time
			theSock = new Socket();
			start = System.currentTimeMillis();
			System.out.println("start:"+start);
			theSock.connect(sockaddr, timeoutMs);
			end = System.currentTimeMillis();
			System.out.println("end:"+end);
		} catch (UnknownHostException e) {
			start = -1;
			end = -1;
		} catch (SocketTimeoutException e) {
			start = -1;
			end = -1;
		} catch (IOException e) {
			start = -1;
			end = -1;
		} finally {
			if (theSock != null) {
				try {
					theSock.close();
				} catch (IOException e) {}
			}
			if ((start != -1) && (end != -1)) {
				total = end - start;
			}
		}
		return total; //returns -1 if timeout
	}
	
	public static void main(String[] args) {
		int port = 10000;
		int timeout = 1000;
		byte[] addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)1};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)5};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)6};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)7};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)8};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)9};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)10};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)11};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)12};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)5};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)6};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)7};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)8};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)9};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)10};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)11};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)12};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
		System.out.println(Ping2.testConn(addr, port, timeout));
		addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
		System.out.println(Ping2.testConn(addr, port, timeout));
	}
}


Erg:
Code:
/172.16.16.1
start:1190027226125
-1
/172.16.16.5
start:1190027227140
-1
/172.16.16.6
start:1190027228140
-1
/172.16.16.7
start:1190027229140
-1
/172.16.16.8
start:1190027230140
-1
/172.16.16.9
start:1190027231140
-1
/172.16.16.10
start:1190027232140
-1
/172.16.16.11
start:1190027233156
-1
/172.16.16.12
start:1190027234156
-1
/172.16.16.20
start:1190027235156
end:1190027235156
0
/172.16.16.21
start:1190027235156
-1
/172.16.16.22
start:1190027236171
-1
/172.16.16.20
start:1190027237171
end:1190027237796
625
/172.16.16.20
start:1190027237796
end:1190027237796
0
/172.16.16.21
start:1190027237812
-1
/172.16.16.22
start:1190027238812
-1
/172.16.16.20
start:1190027239812
end:1190027239812
0
/172.16.16.21
start:1190027239828
-1
/172.16.16.22
start:1190027240828
-1
/172.16.16.20
start:1190027241828
end:1190027241828
0
/172.16.16.21
start:1190027241828
-1
/172.16.16.22
start:1190027242828
-1
/172.16.16.5
start:1190027243828
-1
/172.16.16.6
start:1190027244828
-1
/172.16.16.7
start:1190027245828
-1
/172.16.16.8
start:1190027246828
-1
/172.16.16.9
start:1190027247828
-1
/172.16.16.10
start:1190027248828
-1
/172.16.16.11
start:1190027249828
-1
/172.16.16.12
start:1190027250828
-1
/172.16.16.20
start:1190027251828
-1
/172.16.16.21
start:1190027252843
-1
/172.16.16.22
start:1190027253843
-1
/172.16.16.20
start:1190027254843
end:1190027254843
0
/172.16.16.20
start:1190027254843
end:1190027254843
0
/172.16.16.21
start:1190027254859
-1
/172.16.16.22
start:1190027255859
-1
/172.16.16.20
start:1190027256859
end:1190027256859
0
/172.16.16.21
start:1190027256859
-1
/172.16.16.22
start:1190027257859
-1
/172.16.16.20
start:1190027258859
-1
/172.16.16.21
start:1190027259859
-1
/172.16.16.22
start:1190027260859
-1

also so ganz versteh ich mein derzeitiges problem nicht...
ich hab nur mitbekommen das wenn ich den timeout höher setzte der fehler seltener auftritt...


also kurze erklärung meines problems:
ich hab anhand des letzten links von Slater mir ne variante rausgesucht die eigentlich ganz gut funktioniert...
aber leider nur meistens und net immer...

ich hab euch den code und die ausgabe gepostet...
bei ip: 172.16.16.20 müsste er eigentlich immer eine endzeit zurück geben, macht er aber leider net... woran kann das liegen?

mfg Kala
 
Zu deinem anfänglichen Problem ganz oben:

Code:
Socket socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(ip, port);
 
und je meher anfragen das werden um so schlechter wird die zeit die er braucht um nen socket auf und zu zu machen und da ich ja meine zeit net anpass läuft der irgendwann raus .... und dadurch zeigt der das später gar net mehr richtig an
 
Wieso das?
Deine Anfragen laufen doch nacheinander ab und nicht parralel.
Warum sollten die langsamer werden?

Gruß
Sparrow


Edit:
Was genau bezweckst du denn eigentlich?
Denn deine eigentliche Frage (wie man den Timout vor dem Connect ändert) ist ja eigentlich beantwortet.
Mir ist dein jetziges Problem irgendwie noch nicht ganz klar geworden.
 
ja das frag ich mich auch ...
aber ich hab einfach mal noch 20 dazu gepackt und anhand der kamera die ich auf der anderen seite von der 172.16.16.20 sitzen hab seh ich ja wann er sich connectet und wann net ... und es wird immer langsamer also bei 40 abfragen in meinem programm is der zeitversatz schon so groß:

1 anfrage programm -> 1 anzeige auf kamera

.
.
.
35. anfrage programm -> 33. anzeige auf kamera

und noch schlimmer ... also der thread is schon viel weiter als er eigentlich sein dürfte...
 
edit:

also ich hab nen netzwerk:
172.16.16.X

in diesem netzwerk sind an verschiedenen ips kameras angeschlossen

z.b. auf der 20 (hab derzeit nur eine dran sollen aber mehr werden)

so und nun will ich sowas wie ne geräte detection pasteln... also ich will das netzwerk abfragen ob da so ne kamera is .. und wenn ja soll sie dann später mit ip und namen angezeigt werden...

auf der kamera läuft nen programm welches ich abfragen kann... also wenn ich weis hier an der ip is was, schick ich als nächstes meinen befehl runter der den namen der kamera erfragt und so weiter
 
Code:
} finally { 
         if (theSock != null) { 
            try { 
               theSock.close(); 
            } catch (IOException e) {} 
         } 
         if ((start != -1) && (end != -1)) { 
            total = end - start; 
         } 
      }

wird doch gemacht
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben