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.
Related
What is port I can use for someone can connect me and get message connection is established ?
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"),true);
printWriter.println("Connection is established");
Thank you
Welcome to SO Deny
The port is for you (the server) to decide.
The client needs to know what port to connect to, so it can get a response.
For your question, just don't pick some port that currently your computer is using will be fine. Such as like 20 or 80, those ports are using in real http data transferring
In the below codes, your can notice that I just randomly pick a port, to note that, both ports in sender and receiver have to be the same, otherwise, you will not get the message.
This one is TCPSender.py
from socket import *
serverName = 'localhost'
serverPort = 8797
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input('Input lowercase sentence:')
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ('From Server:', modifiedSentence)
clientSocket.close()
This one is TCP Receiver.py
from socket import *
serverName = 'localhost'
serverPort = 8797
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print ('The server is ready to receive')
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
print(sentence)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
I have an assignment where I have to build a Client-Server communication using Java. So what I did was building a Client-Server connect using Sockets, the following way:
//Client code
Socket socket = new Socket("127.0.0.1", 4999);
//Server code
ServerSocket ss = new ServerSocket(4999);
Socket socket = ss.accept();
System.out.println("Client is connected");
For now, the communication between the client and the server is successful, I am able to send messages from client to server and vice versa. but the main problem is storing data on the server host, we were asked to store relevant data on the server host so the server could send it to the client.
Can someone please explain to me how to store data on the server and how to retrieve it ??
Thanks
To read from the server socket:
ServerSocket server = new ServerSocket(port);
Socket socket = server.accept();
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//Do whatever you want with the DataInputStream
Source: https://www.baeldung.com/java-inputstream-server-socket
Then we can move to the input stream processing, here is a sample:
...
int i;
char c;
...
try{
// reads till the end of the stream
while((i = in .read())!=-1) {
// converts integer to character
c = (char)i;
// prints character
System.out.print(c);
}
} catch(Exception e){
//Handle....
} finally {
// Close stream...
}
Source: https://www.tutorialspoint.com/java/io/inputstream_read.htm#:~:text=io.-,InputStream.,the%20returned%20value%20is%20%2D1.
For writing to the socket (the response to the client), just follow the following tutorial, no need to repeat it here:
http://www.avajava.com/tutorials/lessons/how-do-i-write-a-server-socket-that-can-handle-input-and-output.html
I hope it helps
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
Client
Socket socket = new Socket("ip", 5555);
PrintStream out = new PrintStream(socket.getOutputStream());
out.println("Hello Server!");
out.close();
socket.close();
Server
ServerSocket serverSocket = new ServerSocket(5555);
while (true) {
//keep listening
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
String line = in.readLine();
System.out.println(line);
in.close();
socket.close();
}
if ignore concurrency issue, is the way to close sockets connection correct?
if ignore concurrency issue, is the way to close sockets connection correct?
Yes, but you don't need to close the socket if you've already closed the output stream.
if client closed socket immediately after sent data, will it cause some exception like 'socket is close' while server tries to read data from stream?
No. 'Socket is closed' means you closed the socket and then continued to use it. As long has the client has read everything the server is going to send, the client can close the socket: the server will read all the data the client has sent, and then get end-of-stream on the next read.
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