Week 77 — How can one create a TCP connection in a Java application?

Question of the Week #77
How can one create a TCP connection in a Java application?
7 Replies
Eric McIntyre
Eric McIntyre6mo ago
The Socket class can be used to connect to a specified host/IP and port combination via TCP: A connection can be established using the Socket constructor and data can be read/written using Socket#getInputStream/Socket#getOutputStream.
String host = "example.com";
int port = 80;
try(Socket sock = new Socket(host, port);
Writer pw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), StandardCharsets.UTF_8));
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream(), StandardCharsets.UTF_8))){
pw.write("GET / HTTP/1.1\r\n");
pw.write("Host: example.com\r\n");
pw.write("Connection: close\r\n\r\n");
pw.flush();
br.lines()
.forEach(System.out::println);
}
String host = "example.com";
int port = 80;
try(Socket sock = new Socket(host, port);
Writer pw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), StandardCharsets.UTF_8));
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream(), StandardCharsets.UTF_8))){
pw.write("GET / HTTP/1.1\r\n");
pw.write("Host: example.com\r\n");
pw.write("Connection: close\r\n\r\n");
pw.flush();
br.lines()
.forEach(System.out::println);
}
Similarly, it is possible to accept TCP connections using the ServerSocket class. One can create a ServerSocket listening for requests on some port and accept incoming requests by calling the accept method. This method returns a Socket that can be used for communicating with the connected client. The following code listens for a connection on port 1337 and sends back all data it receives in that connection:
try(ServerSocket serv = new ServerSocket(1337)){
try(Socket sock = serv.accept();
OutputStream os = sock.getOutputStream();
InputStream is = new BufferedInputStream(sock.getInputStream())){
is.transferTo(os);
}
}
try(ServerSocket serv = new ServerSocket(1337)){
try(Socket sock = serv.accept();
OutputStream os = sock.getOutputStream();
InputStream is = new BufferedInputStream(sock.getInputStream())){
is.transferTo(os);
}
}
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre6mo ago
What you would especially need for this TCP connection in Java is to make use of the Socket and ServerSocket classes. The Socket class is used for the client and the server to identify each other and the ServerSocket is for the server to hold a server. Down below is an image of making the ServerSocket in the Server class to host the connection and underneath that is an image of the client class identifying the Server with the use of the Socket class.
Eric McIntyre
Eric McIntyre6mo ago
No description
Eric McIntyre
Eric McIntyre6mo ago
Submission from hypyrcube
No description
Eric McIntyre
Eric McIntyre6mo ago
There are two methods in java to establis a connection. 1. connection less (UDP) 2. connection oriented(TCP) this can be implemented using socket and serversocket class resided in java.net package.
Eric McIntyre
Eric McIntyre6mo ago
we need to write two programs, cliend side and serversocket which also required inputdatastream and outputdatastream
Submission from chandrashekhar_
Eric McIntyre
Eric McIntyre6mo ago
Socket and ServerSocket are used for TCP connections. The connection is established when creating a Socket instance using a constructor that directly connects to one or by using a constructor that creates an unconnected Socket and then by calling Socket#connect. To create a server that accepts tcp connections you can use the ServerSocket and its ServerSocket#accept method.
Submission from squidxtv
Want results from more Discord servers?
Add your server