java - 使用 Java 和 Dynamic C 通过 WiFi 进行套接字连接

标签 java sockets wifi tcpclient tcplistener

好吧,我在通过 WiFi 发送数据时遇到了一些小问题。我有一个 Rabbit RCM5400W WiFi 板,我需要向它发送数据。我用 Java 创建了一些客户端/服务器套接字示例,这些示例在本地主机端口 5000 上运行并且能够发送和接收数据。我现在正在尝试创建一个通过 WiFi 运行的程序,该程序将回显客户端 (Java) 发送到服务器 (Dynamic C) 的任何信息。到目前为止,我的所有连接要么超时,要么抛出“java.net.ConnectException:连接被拒绝”。任何连接这两个的帮助将不胜感激。

动态C服务器

     #class auto

     #define TCPCONFIG             1
     #define _PRIMARY_STATIC_IP    "10.10.6.100"
     #define _PRIMARY_NETMASK      "255.255.255.0"
     #define MY_GATEWAY            "10.10.6.1"
     #define MY_NAMESERVER         "10.10.6.1"
     #define IFC_WIFI_SSID         "rabbitTest"
     #define IFC_WIFI_ROAM_ENABLE  1
     #define IFC_WIFI_ROAM_BEACON_MISS  20
     #define IFC_WIFI_CHANNEL      1
     #define IFC_WIFI_MODE         IFPARAM_WIFI_ADHOC
     #define IFC_WIFI_REGION       IFPARAM_WIFI_REGION_AMERICAS
     #define IFC_WIFI_ENCRYPTION   IFPARAM_WIFI_ENCR_NONE

#use "dcrtcp.lib"


#define PORT 5000


void main()
{
int bytes_read;
/*
    Unless STDIO_ENABLE_LONG_STRINGS is defined, printf() has max 127 bytes
    it can output.  For this sample, we'll read in a maximum of 100 bytes
    at a time.
*/
char    buffer[100];
tcp_Socket socket;

// Start network and wait for interface to come up (or error exit).
sock_init_or_exit(1);

while(1) {
    tcp_listen(&socket,PORT,0,0,NULL,0);

    printf("Waiting for connection...\n");
    while(!sock_established(&socket) && sock_bytesready(&socket)==-1)
        tcp_tick(NULL);

    printf("Connection received...\n");

    do {
        bytes_read=sock_fastread(&socket,buffer,sizeof(buffer)-1);

        if(bytes_read>0) {
            buffer[bytes_read]=0;
            printf("%s",buffer);
            sock_write(&socket,buffer,bytes_read);
        }
    } while(tcp_tick(&socket));

    printf("Connection closed...\n");
 }
} 

Java 客户端

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client implements Runnable {
// The socket connecting us to the server

private Socket socket;
// The streams we communicate to the server; these come
// from the socket
private DataOutputStream dout;
private DataInputStream din;

// Constructor
public Client(String host, int port) {

// Connect to the server
    try {
// Initiate the connection
            socket = new Socket(host, port);
// We got a connection! Tell the world
            System.out.println("connected to " + socket);
// Let's grab the streams and create DataInput/Output streams
// from them
            din = new DataInputStream(socket.getInputStream());
            dout = new DataOutputStream(socket.getOutputStream());
// Start a background thread for receiving messages
        new Thread(this).start();
    } catch (IOException ie) {
        System.out.println(ie);
    }
    //create a variable to hold how many times we send a message
    int counter = 1;

    //continuously accept user messages from console
    while (true) {
        Scanner kb = new Scanner(System.in);
        //we only want to display this message on startup 
        if (counter == 1) {
            System.out.println("Send:");
        }
        processMessage(kb.nextLine());
        counter++;
    }
}

// Gets called when the user types something
private void processMessage(String message) {
    try {
// Send it to the server
        dout.writeUTF(message);     
    } catch (IOException ie) {
        System.out.println(ie);
    }
}
// Background thread runs this: show messages from other window

@Override
public void run() {
    try {
        // Receive messages one-by-one, forever
        while (true) {
           // Get the next message
            String message = din.readUTF();
           // Print it to our text window
            System.out.println("Recieved: " + message);
           //add a new line for user input
            System.out.println("Send:");
        }
    } catch (IOException ie) {
        System.out.println(ie);
    }
   }
 }

Java RunClient 程序

public class ClientRun {

public static void main(String[] args) {
    //we will run the client on port 5000, the same as the server
    int port = 5000;
    //create a new client on WiFi Router port 5000
    new Client("10.10.6.100",port);
 }
}

最佳答案

如果出现“连接超时”,则可能意味着客户端无法将任何网络数据包发送到服务器。如果您收到“连接被拒绝”,则意味着客户端能够通过数据包,但无论如何都没有任何内容监听新连接。

无论哪种情况,问题很可能不是在 Java 代码中,而是在您的网络设置中;例如以您在两端配置 WiFi 网络、网络接口(interface)、路由和/或防火墙和数据包过滤器的方式。

您可能应该从简单的事情开始:

  • 您可以从客户端的命令行“ping”服务器吗?

  • 你能从命令行建立 TCP/IP 网络连接吗?例如使用 telnetnc 连接到服务器端口?

关于java - 使用 Java 和 Dynamic C 通过 WiFi 进行套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11363750/

相关文章:

java - tcp 中的 IOUtils.copy()

c - Recv() 跳过字节

python - 关闭没有 socket.close() 的 UDP 套接字

ios - 使用 Multipeer Connectivity wifi/蓝牙发送 UISlider 值

java - 服务器启动时的调用方法

java - 如何在一个类的方法中运行所有内容?

java - 在 Windows 8 上将 java 6 更新到 java 7

sockets - 3G安全连接吗?

android - Worklight - Wifi 中的连接触发器引发错误

java - 在 JSF 2.0 中检索其他组件的客户端 ID