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
Related
I'm trying to create a server in Java using Sockets. I create a ServerSocket using ServerSocket serverSocket = new ServerSocket(port); and then attempt to connect using Socket clientSocket = serverSocket.accept();. I send the request through Postman.
My issue is that whenever I send a request, I get the output:
New client connected
input: null
closed!
New client connected
input: PUT / HTTP/1.1
closed!
I'm confused as to why the client connects twice, and why the first request is always empty.
Full code:
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected");
BufferedReader br = new BufferedReader(new InputStreamReader((clientSocket.getInputStream())));
String input = br.readLine();
System.out.println("input: " + input);
clientSocket.close();
System.out.println("closed!");
}
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'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 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
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.