Zdravim,
snazim se v Jave naprogramovat jednoduche socketovou aplikaci (klient a server).
Server:
StartServer.java:
package server;
import java.io.IOException;
public class StartServer {
/**
* @param args
* @throws IOException
*/
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;
public class SitServer {
// http://www.kodejava.org/…les/216.html
private String host;
private int port;
private ServerSocket server;
public SitServer(String host, int port) throws IOException {
this.host = host;
this.port = port;
server = new ServerSocket(port);
}
public void handleConnection() {
System.out.println("Waiting for client message...");
// The server do a loop here to accept all connection initiated by the
// client application.
while (true) {
try {
Socket socket = server.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");
}
//System.out.println("Message Received: " + message);
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;
public class SitKlient {
private int port;
private String host;
private Socket klient;
public SitKlient(String host, int port) throws UnknownHostException, IOException {
this.host = host;
this.port = port;
klient = new Socket(host, port);
}
public void SocketWrite(String text) throws IOException {
// Send a message to the client application
ObjectOutputStream oos = new ObjectOutputStream(klient.getOutputStream());
//oos.writeObject("Hello There...");
oos.writeObject(text);
oos.close();
}
public String SocketRead() throws IOException, ClassNotFoundException {
// Read and display the response message sent by server application
ObjectInputStream ois = new ObjectInputStream(klient.getInputStream());
String message = (String) ois.readObject();
//System.out.println("Message: " + message);
ois.close();
return message;
}
public void SocketZavrit() throws IOException {
klient.close();
}
}
Spustim server a pote klient a spadne to s:
Waiting for client message...
Message Received: ABC ABC
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1857)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1766)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1273)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1411)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1557)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at server.ConnectionHandler.run(SitServer.java:80)
at java.lang.Thread.run(Thread.java:722)
Nevite, kde delam chybu? Vzdyt je videt, ze server zpravu prijme.
Dalo by se nejak pri spusteni vyhazet ty vyjimky throws?
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());
}
Zdrojovy kod: http://www.uloz.to/…cket-src-zip
Dekuji za radu :-)