java - not able to connect through socket -
i trying connect 2 computers using socket connection. tried code on same computer entering own ip address. works. it's not working in between 2 computer @ home. similar code did worked on college computers. server side code
import java.io.ioexception; import java.net.serversocket; import java.net.socket; public class testserver { public static void main(string[] args) { serversocket listener = null; try { listener = new serversocket(7689); socket other = listener.accept(); system.out.println("connected"); } catch (ioexception e) { e.printstacktrace(); } } }
this client side code
import java.io.ioexception; import java.net.socket; import java.net.unknownhostexception; import java.util.scanner; public class testclient { public static void main(string[] args) { scanner sc = new scanner(system.in); system.out.println("enter ip"); string ip=sc.next(); try { socket s = new socket(ip,7689); system.out.println("connected"); } catch (ioexception e) { e.printstacktrace(); } } }
on server computer typed ifconfig got line
eth0 link encap:ethernet hwaddr f0:1f:af:17:26:db broadcast multicast mtu:1500 metric:1 rx packets:0 errors:0 dropped:0 overruns:0 frame:0 tx packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:0 (0.0 b) tx bytes:0 (0.0 b) lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host loopback running mtu:65536 metric:1 rx packets:51 errors:0 dropped:0 overruns:0 frame:0 tx packets:51 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:5083 (5.0 kb) tx bytes:5083 (5.0 kb) wlan0 link encap:ethernet hwaddr bc:85:56:49:dc:3d inet addr:192.168.0.48 bcast:192.168.0.255 mask:255.255.255.0 inet6 addr: fe80::be85:56ff:fe49:dc3d/64 scope:link broadcast running multicast mtu:1500 metric:1 rx packets:62 errors:0 dropped:0 overruns:0 frame:0 tx packets:94 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:13422 (13.4 kb) tx bytes:15688 (15.6 kb)
and using ip address server inet addr:127.0.0.1 next started server code nothing happens there
on client side typed ifconfig too
ifconfig eth0 link encap:ethernet hwaddr 9c:b6:54:c5:18:db broadcast multicast mtu:1500 metric:1 rx packets:0 errors:0 dropped:0 overruns:0 frame:0 tx packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:0 (0.0 b) tx bytes:0 (0.0 b) lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host loopback running mtu:65536 metric:1 rx packets:269 errors:0 dropped:0 overruns:0 frame:0 tx packets:269 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:25160 (25.1 kb) tx bytes:25160 (25.1 kb) wlan0 link encap:ethernet hwaddr 80:56:f2:49:dc:0c inet addr:192.168.2.13 bcast:192.168.2.255 mask:255.255.255.0 inet6 addr: fe80::8256:f2ff:fe49:dc0c/64 scope:link broadcast running multicast mtu:1500 metric:1 rx packets:3842 errors:0 dropped:0 overruns:0 frame:0 tx packets:3405 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:2981248 (2.9 mb) tx bytes:1352577 (1.3 mb)
then started client side code on other computer after while comes up
java testclient enter ip 192.168.0.48 java.net.socketexception: invalid argument or cannot assign requested address @ java.net.plainsocketimpl.socketconnect(native method) @ java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:339) @ java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:200) @ java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:182) @ java.net.sockssocketimpl.connect(sockssocketimpl.java:392) @ java.net.socket.connect(socket.java:579) @ java.net.socket.connect(socket.java:528) @ java.net.socket.<init>(socket.java:425) @ java.net.socket.<init>(socket.java:208) @ testclient.main(testclient.java:13)
Comments
Post a Comment