我想在openshift服务器中运行套接字程序。
我的action_hooks / start: -
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:8080
#nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy |& /usr/bin/logshifter -tag diy &
java -cp $OPENSHIFT_REPO_DIR/diy/EchoServer
我的服务器程序: -
import java.net.*;
import java.io.*;
public class EchoServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("xxx.xxx.xx.xx",8080));
}
catch (IOException e)
{
System.err.println("Could not listen on port: 8080.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
我的客户端程序: -
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
String serverHostname = new String ("xxxxxxxxxxx.rhcloud.com");
if (args.length > 0)
serverHostname = args[0];
System.out.println ("Attemping to connect to host " +
serverHostname + " on port 8080.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, 8080);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
System.out.print ("input: ");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
System.out.print ("input: ");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
我的日志文件" diy.log"
[2015-12-22 11:59:03] INFO WEBrick 1.3.1
[2015-12-22 11:59:03] INFO ruby 1.8.7 (2013-06-27) [x86_64-linux]
[2015-12-22 11:59:03] INFO WEBrick::HTTPServer#start: pid=44992 port=8080
[2015-12-22 12:05:36] INFO going to shutdown ...
[2015-12-22 12:05:36] INFO WEBrick::HTTPServer#start done.
我不知道服务器程序是否自动启动但是当我通过ssh运行服务器程序时,程序正在运行但没有响应客户端。
答案 0 :(得分:1)
您似乎正在尝试使用客户端连接到端口8080:
echoSocket = new Socket(serverHostname, 8080);
服务器仍然需要绑定到8080才能从外部访问,但客户端实际上应该连接到80或8000(websockets)。有关如何在OpenShift上路由请求的详细信息,请参阅this diagram。
按下代码时检查终端输出,以查看启动服务器的操作挂钩是否有效。确保the hook file executable。