私はTCP/IP
Javaで取り組んでいます。まず、TCP/IP
それがどのように機能するかを読んで理解します。
私が必要とするもの:-
わかりました、今私はそれを Java で実装したいと思います。port/IP
IPから特定のリクエストに入力を送信しようとしています。そして応答を得る必要があります。
実装方法がわかりません。
ここに私の入力があります:
Destination IP
Destination Port
Input(String or Anything)
これがクライアントに使用する私のコードです。
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System.out.println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
ヒントや参考になさってください。