Zdravim, komunikace pres socket mi uz funguje, ale nyni potrebuji SSL.
Upravil jsem nasledovne kod:
Server:
StartServer.java:
package server;
import java.io.IOException;
public class StartServer {
public static void main(String[] args) throws IOException {
SitServer s = new SitServer("127.0.0.1", 4444);
s.handleConnection();
}
}
SitServer.java:
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
public class SitServer {
// http://www.kodejava.org/…les/216.html
private String host;
private int port;
private ServerSocket server;
private ServerSocket ssocket;
public SitServer(String host, int port) throws IOException {
this.host = host;
this.port = port;
// http://www.exampledepot.com/…/Server.html
ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ssocket = ssocketFactory.createServerSocket(port);
}
public void handleConnection() {
System.out.println("Waiting for client message...");
while (true) {
try {
Socket socket = ssocket.accept();
new ConnectionHandler(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
class ConnectionHandler implements Runnable {
private Socket socket;
public ConnectionHandler(Socket socket) {
this.socket = socket;
Thread t = new Thread(this);
t.start();
}
public void run() {
try
{
// Read a message sent by client application
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
// Send a response information to the client application
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
if (message.equals("ABC")) {
System.out.println("Message Received: ABC " + message);
oos.writeObject("Prijato: " + message + "\n");
} else if (message.equals("DEF")) {
System.out.println("Message Received: DEF " + message);
oos.writeObject("Prijato: " + message + "\n");
} else {
System.out.println("Message Received: JINE... "+ message);
oos.writeObject("Prijato: " + message + "\n");
}
ois.close();
oos.close();
socket.close();
System.out.println("Waiting for client message...");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Klient
StartKlient.java:
package klient;
import java.io.IOException;
import java.net.UnknownHostException;
public class StartKlient {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
SitKlient sk = new SitKlient("127.0.0.1", 4444);
sk.SocketWrite("ABC");
System.out.println(sk.SocketRead());
}
}
SitKlient.java:
package klient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
public class SitKlient {
private Socket klient;
public SitKlient(String host, int port) throws UnknownHostException, IOException {
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket klient = socketFactory.createSocket(host, port);
}
public void SocketWrite(String text) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(klient.getOutputStream());
oos.writeObject(text);
oos.flush();
}
public String SocketRead() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(klient.getInputStream());
String message = (String) ois.readObject();
return message;
}
public void SocketZavrit() throws IOException {
klient.close();
}
}
Hned pri spusteni serveru to spadne s:
Exception in thread "main" java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
at java.net.ServerSocket.bind(ServerSocket.java:328)
at java.net.ServerSocket.<init>(ServerSocket.java:194)
at java.net.ServerSocket.<init>(ServerSocket.java:150)
at javax.net.ssl.SSLServerSocket.<init>(SSLServerSocket.java:84)
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.<init>(SSLServerSocketImpl.java:81)
at com.sun.net.ssl.internal.ssl.SSLServerSocketFactoryImpl.createServerSocket(SSLServerSocketFactoryImpl.java:58)
at server.SitServer.<init>(SitServer.java:26)
at server.StartServer.main(StartServer.java:6)
Kde delam chybu?