I am trying to figure out how to make this piece of code into TCP instead of UDP
DatagramPacket answerDP = null;
answerDP = new DatagramPacket(new byte[110], 110);
What do I use in TCP instead of DatagramPacket?
The same goes for DatagramSocket, what do I use in TCP instead?
DatagramSocket socket = null;
socket = new DatagramSocket();
socket.send(packet);
socket.setSoTimeout(5000); //wait for answar max. 1 sec.
socket.receive(answerDP);
DatagramSocket in TCP is basically ServerSocket
So for example, to initialise it:
ServerSocket welcomeSocket = new ServerSocket(6789);
And the client socket would be something like:
Socket clientSocket = new Socket("localhost", 6789);
Set timeout works the same
socket.setSoTimeout(5000);
See a simple example here
Related
I'm using this code to constantly accept when a new Socket connect to Server:
while (true) {
Socket socket = null;
socket = ss.accept();
System.out.println("A client is connect...\n");
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Thread acceptClient = new Thread(new ClientHandler(socket, in, out));
acceptClient.start();
}
But when a connection is created, I see this in the console:
A client is connect...
A client is connect...
A client is
connect...
I don't understand why this line is displayed 3 times. Can anyone explane?
i'm working on an application to print to my local wifi printers but over 9100 port i have no response, over the 515 i have "connection refused".
the code is this:
Socket test = new Socket();
Socket sock = new Socket(ip,9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.printf("========================\n");
oStream.printf("PROVA STAMPA\n");
oStream.printf("HELLO SLOW SUD\n");
oStream.printf("========================"+"\n");
oStream.close();
sock.close();
and also i have try this:
Socket objSocket = new Socket();
InetSocketAddress objEndPoint = new InetSocketAddress(ip, 9100);
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 6000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("========================\nPROVA STAMPA\nHELLO SLOW SUD\n========================\n").getBytes());
objOutputStream.close();
objSocket.close();
clientSocket = new Socket(args[0], 4442);
ServerSocket serverSocket = new ServerSocket(1111);
client sever sockets
full knowlege about
how we decided port no. and socket no. ???
Socket myClient = new Socket(ipAddress, PortNumber);
I think this will help you
how do I get this code (tcp) to udp in java? Thanks.
ServerSocket servsock2 = new ServerSocket(13367);
Socket sock = servsock2.accept();
System.out.println("Client connected.");
OutputStream os = sock2.getOutputStream();
while(true){
os.write(int_value);
}
The following Java tutorial should get you started: All About Datagrams.
while (true) {
ServerSocket myServerSocket = new ServerSocket(9999);
Socket skt = myServerSocket.accept();
Handling obj = new Handling();
obj.handle(skt);
}
When i first try this it works fine and accepts the Socket but then when it loops back it says the address is in use. How do I fix this?
Don't create a new ServerSocket in the loop - you only need to accept in the loop:
ServerSocket myServerSocket = new ServerSocket(9999);
while (true) {
Socket skt = myServerSocket.accept();
Handling obj = new Handling();
obj.handle(skt);
}