java - Address already in use while using different port numbers to create sockets -



java - Address already in use while using different port numbers to create sockets -

i'm getting next exception (java.net.bindexception: address in use) reason, eventhough i'm incrementing port # before starting new socket it. exception did not occur me before using threads.

the sender should send parts of file (each part using new socket) receiver. programme working , parts received without using threads.

now first part of file received @ receiver server not create sockets after first one.

also, these port #s not used did work before started using threading.

i'm new threading know might using wrong.

thanks in advance!

server class:

public class server { int port = 8500; public server(int x) { (int = 0; < x; i++) { serverthread s = new serverthread(port); s.start(); port++; } } public static void main(string[] args) throws ioexception { datagramsocket socket = new datagramsocket(7499); byte[] buffer = new byte[256]; datagrampacket receivepacket = new datagrampacket(buffer, buffer.length); socket.receive(receivepacket); string message = new string(buffer, 0, receivepacket.getlength()); int xyz = integer.parseint(message); socket.close(); server server = new server(xyz); seek { filejoin f = new filejoin(); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } system.exit(0); }

} serverthread class:

public class serverthread extends thread { private fileevent fileevent1 = null; private int port; serverthread(int port) { this.port = port; run(); } public void run() { seek { createandlistensocket(this.port); } grab (classnotfoundexception | interruptedexception | ioexception e) { e.printstacktrace(); } } public void createandlistensocket(int portx) throws interruptedexception, ioexception, classnotfoundexception { datagramsocket socket1 = new datagramsocket(portx); byte[] incomingdata1 = new byte[1024 * 1000 * 50]; datagrampacket incomingpacket1 = new datagrampacket(incomingdata1, incomingdata1.length); socket1.receive(incomingpacket1); byte[] data1 = incomingpacket1.getdata(); bytearrayinputstream in1 = new bytearrayinputstream(data1); objectinputstream is1 = new objectinputstream(in1); fileevent1 = (fileevent) is1.readobject(); if (fileevent1.getstatus(0).equalsignorecase("error")) { system.exit(0); } createandwritefile(); // writing file hard disk inetaddress ipaddress = incomingpacket1.getaddress(); int porty = incomingpacket1.getport(); string reply = "the file successfuly saved. "; byte[] replybytea = reply.getbytes(); datagrampacket replypacket = new datagrampacket(replybytea, replybytea.length, ipaddress, porty); socket1.send(replypacket); thread.sleep(3000); } public void createandwritefile() { string outputfile1 = fileevent1.getdestinationdirectory(0) + fileevent1.getfilename(0); if (!new file(fileevent1.getdestinationdirectory(0)).exists()) { new file(fileevent1.getdestinationdirectory(0)).mkdirs(); } file dstfile1 = new file(outputfile1); fileoutputstream fileoutputstream1 = null; seek { fileoutputstream1 = new fileoutputstream(dstfile1); fileoutputstream1.write(fileevent1.getfiledata(0)); fileoutputstream1.flush(); fileoutputstream1.close(); system.out.println("output file : " + outputfile1 + " saved "); } grab (filenotfoundexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } }

}

client class:

public class client { private fileevent event1 = null; private string sourcefilepath = "d:/workspace/newudp/image.jpgpart"; private int filenum; private string destinationpath = "e:/udpreceive/"; private int port = 8500; private string hostname = "127.0.0.1"; public client() { } public void createconnection(int x) { seek { inetaddress ipaddress = inetaddress.getbyname(hostname); (int = 0; < x; i++) { datagramsocket socket1 = new datagramsocket(); byte[] incomingdata = new byte[1024]; event1 = getfileevent(0); bytearrayoutputstream outputstream = new bytearrayoutputstream(); objectoutputstream os = new objectoutputstream(outputstream); os.writeobject(event1); byte[] info = outputstream.tobytearray(); datagrampacket sendpacket1 = new datagrampacket(data, data.length, ipaddress, port); socket1.send(sendpacket1); system.out.println("file sent client"); datagrampacket incomingpacket = new datagrampacket( incomingdata, incomingdata.length); socket1.receive(incomingpacket); string response = new string(incomingpacket.getdata()); system.out.println("response server:" + response); port++; } thread.sleep(2000); } grab (unknownhostexception e) { e.printstacktrace(); } grab (socketexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (interruptedexception e) { e.printstacktrace(); } } public fileevent getfileevent(int i) { fileevent fileevent = new fileevent(); sourcefilepath = sourcefilepath + filenum; string filename1 = sourcefilepath.substring( sourcefilepath.lastindexof("/") + 1, sourcefilepath.length()); string path1 = sourcefilepath.substring(0, sourcefilepath.lastindexof("/") + 1); fileevent.setdestinationdirectory(destinationpath, 0); fileevent.setfilename(filename1, 0); fileevent.setsourcedirectory(sourcefilepath, 0); file file1 = new file(sourcefilepath); filenum++; sourcefilepath = "d:/workspace/newudp/image.jpgpart"; if (file1.isfile()) { seek { datainputstream distream = new datainputstream( new fileinputstream(file1)); long len = (int) file1.length(); byte[] filebytes = new byte[(int) len]; int read = 0; int numread = 0; while (read < filebytes.length && (numread = distream.read(filebytes, read, filebytes.length - read)) >= 0) { read = read + numread; } fileevent.setfilesize(len, 0); fileevent.setfiledata(filebytes, 0); fileevent.setstatus("success", 0); } grab (exception e) { e.printstacktrace(); fileevent.setstatus("error", 0); } } else { system.out.println("path specified not pointing file"); fileevent.setstatus("error", 0); } homecoming fileevent; } public static void main(string[] args) throws ioexception { seek { filesplit f = new filesplit(); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } seek { datagramsocket socket = new datagramsocket(); string xyz = "" + filesplit.getj(); system.out.println(xyz); inetaddress serveraddress = inetaddress.getbyname("127.0.0.1"); byte[] sendbytes = xyz.getbytes(); datagrampacket sendpacket = new datagrampacket(sendbytes, sendbytes.length, serveraddress, 7499); socket.send(sendpacket); system.out.println(xyz); client client = new client(); client.createconnection(integer.parseint(xyz)); } grab (ioexception x) { } system.exit(0); }

} stack trace:

java.net.bindexception: address in use: cannot bind @ java.net.dualstackplaindatagramsocketimpl.socketbind(native method) @ java.net.dualstackplaindatagramsocketimpl.bind0(unknown source) @ java.net.abstractplaindatagramsocketimpl.bind(unknown source) @ java.net.datagramsocket.bind(unknown source) @ java.net.datagramsocket.<init>(unknown source) @ java.net.datagramsocket.<init>(unknown source) @ java.net.datagramsocket.<init>(unknown source) @ pack.serverthread.createandlistensocket(serverthread.java:32) @ pack.serverthread.run(serverthread.java:24)

you not using threads here. start server in constructor. when phone call s.start();, calling stub method of thread since run method has different signature, need override it, not overload it, see difference here. other softwares can utilize ports also. need check if free, not utilize it

java sockets ports

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -