java与Java之间的socket通信 建立长链接
时间: 2023-07-08 16:18:06 浏览: 198
Java中的Socket通信可以通过ServerSocket和Socket类来实现。要建立长连接,可以在服务器端和客户端分别使用while循环,不断地接收和发送数据。
服务器端可以使用如下代码来建立长连接:
```
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
// 处理客户端连接
// ...
}
```
客户端可以使用如下代码来建立长连接:
```
Socket socket = new Socket(host, port);
while (true) {
// 发送数据
// ...
// 接收数据
// ...
}
```
在这种情况下,服务器端和客户端会保持长连接,直到其中一方关闭连接。需要注意的是,在长连接中,需要注意及时释放资源,避免资源泄露。
相关问题
java与c 通信_Java与C之间的socket通信 建立长链接
要建立长连接,需要在Java和C端的代码中进行一些修改:
Java端代码:
```java
import java.net.*;
import java.io.*;
public class JavaSocket {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
outputLine = "Hello from Java!";
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
System.out.println("Received from C: " + inputLine);
outputLine = "Hello from Java again!";
out.println(outputLine);
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
C语言端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (1) {
if (recv(sockfd, recvline, MAXLINE, 0) == 0) {
printf("Server terminated prematurely\n");
exit(1);
}
printf("Received from Java: %s", recvline);
printf("Enter message to send to Java: ");
fgets(sendline, MAXLINE, stdin);
send(sockfd, sendline, strlen(sendline), 0);
}
close(sockfd);
exit(0);
}
```
在Java端代码中,我们将while循环放在了try语句块之外,这样就可以在C语言端关闭socket连接后继续向Java端发送消息。在C语言端代码中,我们将while循环改为了一个无限循环,每次接收Java端的消息后,再等待用户输入要发送给Java端的消息。这样就可以实现长连接了。
java与c 通信_Java与C之间的socket通信 代码
以下是一个简单的Java与C之间的socket通信的示例代码:
Java端代码:
```java
import java.net.*;
import java.io.*;
public class JavaSocket {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
outputLine = "Hello from Java!";
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
System.out.println("Received from C: " + inputLine);
outputLine = "Hello from Java again!";
out.println(outputLine);
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
C语言端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (fgets(sendline, MAXLINE, stdin) != NULL) {
send(sockfd, sendline, strlen(sendline), 0);
if (recv(sockfd, recvline, MAXLINE, 0) == 0) {
printf("Server terminated prematurely\n");
exit(1);
}
printf("Received from Java: %s", recvline);
}
close(sockfd);
exit(0);
}
```
这段代码中,Java端通过socket连接到本地的8888端口,向C语言端发送一条消息,并通过输入流接收C语言端的回复。C语言端通过socket连接到本地的8888端口,接收Java端的消息,并向Java端发送一条回复。运行这两个程序,可以看到它们之间的交互。