将JAVA程序作为后台服务运行
我在android中做一个应用程序,它使用套接字编程通过Java服务器程序连接到PC...
我需要将Java服务器程序作为后台服务运行...
任何人都可以帮助我吗???
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
//dataOutputStream.writeUTF("Hello!");
//String dataStream = dataInputStream.readUTF();
// System.out.println("message: " + dataStream);
//dataOutputStream.writeUTF("Hello!");
c++;
dataOutputStream.writeUTF(ss+c);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if( socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Im doing an application in android,which connects to a PC via a Java server program using socket programming...
I need to run the Java server program as a background service...
can anybody pls help me???
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
//dataOutputStream.writeUTF("Hello!");
//String dataStream = dataInputStream.readUTF();
// System.out.println("message: " + dataStream);
//dataOutputStream.writeUTF("Hello!");
c++;
dataOutputStream.writeUTF(ss+c);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if( socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要“包装”您的 Java 应用程序以作为服务运行。这将允许 java 程序像其他服务一样执行。有一些工具可用于此目的。看
Java 服务包装 和 YAJSW 了解更多详情。
You will need to "wrap" your java application to run as a service. This will allow the java program to execute just like other services. There are tools available for this purpose. See
Java Service Wrapper and YAJSW for more details.
是的,您可以使用 Java Service Wrapper 来运行 java 应用程序。
yes, you can run the java application by use Java Service Wrapper.
尝试将 Java 代码设置为批处理文件;如果适用于 Windows 操作系统...
Try setting the Java code as a Batch file;if it is for a Windows OS...