我有 PC ,其中有一个微控制器的主板。 PC和板通过LAN电缆连接。 我有一个PC和板的IP地址和端口号。我想从我的PC发送一个字符串到董事会。我知道我们必须使用套接字程序。 我把我的PC作为客户端,并将其作为服务器。 当我运行客户端程序时,由于 连接被拒绝 ,我收到错误。 两者的端口号是相同的。 (这都是关于发送 TCP 数据包)。 当我使用相同的客户端程序与作为服务器的其他计算机进行通信时,它可以工作。 我有用c ++编写的代码,但我想用java编写代码。
这方面的代码会有所帮助 提前致谢
答案 0 :(得分:1)
以下是取自java doc的示例:
服务器程序:
/** EchoServer.java - Echoes the input from client, this needs to be
* executed from microcontroller
*/
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java EchoServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
try {
ServerSocket serverSocket =
new ServerSocket(Integer.parseInt(args[0]));
Socket clientSocket = serverSocket.accept();
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("Received: "+inputLine);
System.out.println("Echoing: "+inputLine);
out.println(inputLine);
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
在微控制器中执行EchoServer.java,如下所示:
javac EchoServer.java
java EchoServer 5541
客户计划:
/** EchoClient.java - Sends strings to server
* and displays the echoed string from server
*/
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try {
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
}
}
在PC中执行EchoClient.java,如下所示:
javac EchoClient.java
java EchoClient 110.21.56.74 5541
此处,110.21.56.74应替换为微控制器的IP地址。
按Ctrl+C
自从我从事C套接字编程以来,已经有很长一段时间了。此link的以下程序按预期工作。
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
printf("Received : %s\n",client_message);
printf("Echoing : %s\n",client_message);
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
请注意,此C服务器程序使用8888作为端口号。