SSL Client/Server error - java

I have a simple client/server program that authenticates using SSL. The program compiles and run free from error but when I send a message form the client side to the server the following error occurs in client side:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2011)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1113)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:735)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:254)
at EchoClient.main(EchoClient.java:32)
And this error is shown in the server side:
javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:292)
at sun.security.ssl.ServerHandshaker.chooseCipherSuite(ServerHandshaker.java:1014)
at sun.security.ssl.ServerHandshaker.clientHello(ServerHandshaker.java:731)
at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:213)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:969)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:904)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:916)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at EchoServer.main(EchoServer.java:26)
My server code is as follows:
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public
class EchoServer {
public static void main(String[] arstring) {
try {
SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
sslsocket.setEnabledProtocols(new String[]{"SSLv3", "TLSv1"});
InputStream inputstream = sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println(string);
System.out.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
I run it with the following command: java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=raina141 EchoServer
And my Client code is as follows:
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
public
class EchoClient {
public static void main(String[] arstring) {
// System.out.println("Type in a message to send to Server");
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);
sslsocket.setEnabledProtocols(new String[]{"SSLv3", "TLSv1"});
InputStream inputstream = System.in;
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
OutputStream outputstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
String string = null;
// if ((string = bufferedreader.readLine()) == "end"){
// bufferedwriter.write("Communication closed by Client");
// bufferedwriter.flush();
//}
while ((string = bufferedreader.readLine()) != null) {
bufferedwriter.write(string + '\n');
bufferedwriter.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
I run it with the following command:
java -Djavax.net.ssl.trustStore=mySrvKeystore -Djavax.net.ssl.trustStorePassword=raina141 EchoClient

Related

Unsupported or unrecognized SSL message, cannot be read

I am trying to display the date on an HTTPS website via an SSL server.
I am getting an error thrown on line 31 (I have marked where it is).
I reckon it might be to do with the browser and how it is set up. Since the error is coming from an unsupported message.
Code:
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Date;
public class SSLServer {
final static String pathToStores = "keys";
final static String keyStoreFile = "server-key.pem";
final static String password = "";
final static int port = 8080;
static boolean debug = false;
void doServerSide() throws Exception {
SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port);
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
InputStream inputStream = sslSocket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
///////////////////// exception thrown on the line below /////////////////////
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
OutputStream outputStream = sslSocket.getOutputStream();
String httpResponce = "HTTP/1.1 200 OK\r\n\r\n" + new Date();
outputStream.write(httpResponce.getBytes(StandardCharsets.UTF_8));
sslSocket.close();
}
public static void main(String[] args) throws Exception {
String trustFilename = pathToStores + "/" + keyStoreFile;
System.setProperty("java.net.ssl.keyStore", trustFilename);
System.setProperty("javax.net.ssl.keyStorePassword", password);
if (debug) System.getProperty("java.net.debug", "all");
new SSLServer().doServerSide();
}
}
Exception:
Exception in thread "main" javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
at java.base/sun.security.ssl.SSLSocketInputRecord.handleUnknownRecord(SSLSocketInputRecord.java:451)
at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:175)
at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1497)
at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1403)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:441)
at java.base/sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:903)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:994)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:178)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
at SSLServer.doServerSide(SSLServer.java:31)
at SSLServer.main(SSLServer.java:51)
I am using firefox and chrome for testing.
Thanks :)

Java Sockets, java.net.SocketException: Connection reset, because of BufferedWriter/Reader?

This is just a very Simple Server/Client application that i have written in Java with the intention using BufferedReader and BufferedWriter only.
I dont get any Errors when i start the Server.
But when the Client tries to send a message to the server:
java.net.SocketException: Connection reset
appears. I dont know why this happens but i think it could have to do something with the BufferedWriter maybe.
I searched in the Internet but i haven't found anything usefull specific to this.
Server.java
import java.io.*;
import java.net.*;
public class Server {
public Server(int port) {
try {
ServerSocket seso = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Server listening on port " + port);
Socket client1 = seso.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client1.getInputStream(), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client1.getOutputStream(), "UTF-8"));
String s = null;
while((s = reader.readLine()) != null) {
System.out.println("Client: " + s);
}
writer.close();
reader.close();
} catch (Exception e) {e.printStackTrace();}
}
public static void main(String args[]) {
new Server(4444);
}
}
Client.java
import java.io.*;
import java.net.*;
public class Client {
public Client(String serverip) {
try {
Socket client = new Socket(serverip, 4444);
System.out.println("Client connected with: " + serverip);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "UTF-8"));
writer.write("Hello Server");
writer.flush();
}catch(Exception e){e.printStackTrace();}
}
public static void main(String args[]) {
new Client("localhost");
}
}
And when i run the server followed by the client i get this from the console:
Server started
Server listening on port 4444
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 9 seconds)
If i add:
writer.newLine();
after
writer.write("Hello Server");
in Client.java i still get:
Server started
Server listening on port 4444
Client: Hello Server
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 2 seconds)

ssl socket multithreaded server exception

I want to create an ssl socket for a multithreaded server, but when a second client is connected I get some exceptions.
Here is my server code:
public class Master implements Runnable{
public static SSLSocket sslSocket = null;
public static SSLServerSocket sslServerSocket =null;
public static SSLServerSocketFactory sslServerSocketfactory ;
Master(SSLSocket s) {
this.sslSocket = s;
}
public static void main(String args[]) throws Exception {
System.out.println("Listening");
sslServerSocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(7777);
while (true)
{
sslSocket = (SSLSocket) sslServerSocket.accept();
sslSocket.setEnabledCipherSuites(sslServerSocketfactory.getSupportedCipherSuites());
System.setProperty("javax.net.ssl.keyStore", "Master_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "passwd1");
System.out.println("Connected");
new Thread(new Master(sslSocket)).start();
}
}
public void run() {
synchronized(this){
do{
try{
//here is the error
ObjectOutputStream objout = new ObjectOutputStream(sslSocket.getOutputStream());
ObjectInputStream objin = new ObjectInputStream(sslSocket.getInputStream());
/* code */
} catch (Exception ex) {
Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);}
}while(true);
}
}
}
And my client code is the following
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 7777);
sslsocket.setEnabledCipherSuites(sslsocketfactory.getSupportedCipherSuites());
System.setProperty("javax.net.ssl.keyStore", "Client_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "passwd2");
try {
do {
ObjectOutputStream objout = new ObjectOutputStream(sslsocket.getOutputStream());
ObjectInputStream objin = new ObjectInputStream(sslsocket.getInputStream());
/* code*/
} while (true);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
The exceptions I get are:
SEVERE: null
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1541)
at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1553)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:71)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
at master.Master.run(Master.java:233)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:292)
at sun.security.ssl.ServerHandshaker.chooseCipherSuite(ServerHandshaker.java:1035)
at sun.security.ssl.ServerHandshaker.clientHello(ServerHandshaker.java:738)
at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:221)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
... 5 more
can someone give me a solution?
public static SSLSocket sslSocket = null;
The problem is here. There is absolutely no reason for making this variable static. Don't use static unless you know exactly why you are doing so.

Client server messages in a loop

I want to send two messages at least from the client to the server and the server then responds to these messages. However only one message is sent from the client and the server also responds to one any idea where i am wrong
This is my server.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String sub = br.readLine();
//string parsing
String delims = "[.]";
String[] tokens = sub.split(delims);
System.out.println("You have subscribed to "+tokens[1]);
String returnMessage="Subscription message received.";
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
InputStream iss = socket.getInputStream();
InputStreamReader isrr = new InputStreamReader(iss);
BufferedReader brr = new BufferedReader(isrr);
String subb = brr.readLine();
System.out.println("Regular message received from client is "+subb);
OutputStream oss = socket.getOutputStream();
OutputStreamWriter osww = new OutputStreamWriter(oss);
BufferedWriter bww = new BufferedWriter(osww);
bww.write(subb);
System.out.println("Message sent "+subb);
bww.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
And this is my client.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the subscription message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String subscribe= "Subscribe.nust";
String sendMessage = subscribe + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
//Send the regualar message to the server
OutputStream oss = socket.getOutputStream();
OutputStreamWriter osww = new OutputStreamWriter(oss);
BufferedWriter bww = new BufferedWriter(osww);
String regular= "Because of the APS Peshawar attack that took place on 16th December 2014, nust is facing security issues.";
String sendMessage2 = regular + "\n";
bww.write(sendMessage2);
bww.flush();
System.out.println("Message sent to the server : "+sendMessage2);
//Get the return message from the server
InputStream iss = socket.getInputStream();
InputStreamReader isrr = new InputStreamReader(iss);
BufferedReader br1 = new BufferedReader(isrr);
String msg = br1.readLine();
System.out.println("Message received from the server : " +msg);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Don't keep creating new BufferedReaders/Writers on the same socket. Use the same ones for the life of the socket. You're losing data in their buffers.

Is it possible to decode bytes on a server which were sent from a client as a byte array?

I'm just trying to test sending bytes over a TCP socket connection, I know it wasn't really meant for that but I'm just trying to figure out whether this is possible or not
what i'm trying to do:
get bytes from a string on client
sent it as bytes to the server
get the bytes on the server and decode it back to the original string
Client:
package ByteClientServer;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
public class Client {
String hostAddress = "localhost";
int port = 1010;
public Client()
{
try {
Socket socket = new Socket(hostAddress, port);
String test = "hello"; //dycrypt bytes from this string on server side
byte[] byteArray = test.getBytes();
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.write(byteArray);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
Server:
package ByteClientServer;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)
{
try
{
ServerSocket server = new ServerSocket(1010);
server.setSoTimeout(0);
Socket connectionToClient = server.accept();
InputStream is = connectionToClient.getInputStream();
DataInputStream dis = new DataInputStream(is);
byte[] data = dis.readUTF().getBytes();
//dis.readFully(data, 0, data.length);
String s = new String(data);
System.out.println(s);
}
catch(IOException e)
{
e.printStackTrace();
//System.err.println("Server was terminated.");
}
}
}
it doesn't like this line on server:
byte[] data = dis.readUTF().getBytes();
and throws the exception:
java.net.SocketException: Connection reset at
java.net.SocketInputStream.read(Unknown Source) at
java.net.SocketInputStream.read(Unknown Source) at
java.io.DataInputStream.readFully(Unknown Source) at
java.io.DataInputStream.readUTF(Unknown Source) at
java.io.DataInputStream.readUTF(Unknown Source) at
ByteClientServer.Server.main(Server.java:21)
If you want to use readUTF then you need to use writeUTF. if you want to just write bytes, then you need to read just bytes.
You are writing bytes with default encoding then reading it as UTF-8 encoding. Thats the issue.
Here is John Skeets blog explaining how to debug these errors and some pitfalls
I came up with a simple workaround
Client:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args)
{
String hostAddress = "localhost";
int port = 8080;
Socket socket = null;
String test = "hello"; //decode bytes from this string on the server
byte[] byteArray = test.getBytes();
try
{
socket = new Socket(hostAddress, port);
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.write(byteArray, 0, byteArray.length);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Server:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws SocketException
{
try
{
ServerSocket server = new ServerSocket(8080);
server.setSoTimeout(0);
Socket connectionToClient = server.accept();
InputStream is = connectionToClient.getInputStream();
DataInputStream dis = new DataInputStream(is);
int buffersize = connectionToClient.getReceiveBufferSize();
byte[] bytes = new byte[buffersize];
if(dis.read(bytes) > 0)
{
String s = new String(bytes);
System.out.print(s);
}
dis.close();
server.close();
}
catch(IOException e)
{
e.printStackTrace();
System.err.println("Server was terminated.");
}
}
}

Categories

Resources