Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 3 months ago.
Connection TCP/IP Mbed - PC Problem
Hi everyone,
I'm trying to do some TCP/IP connection between PC and a mbed (NPX LPC 1768) but i'm having some problems.
My first step is simple, just send from pc to mbed some string and receive other. But i'm having problems with the connect. My code in Mbed is this:
#include "mbed.h" #include "EthernetInterface.h" #include "string.h" DigitalOut myled(LED1); DigitalOut myled2(LED2); ///Just to test sending 1010 void passTeclado(char *pass){ if (true)sprintf(pass,"1"); if (true)sprintf(pass,"%s0",pass); if (true)sprintf(pass,"%s1",pass); if (true)sprintf(pass,"%s0",pass); } bool compare(char passReceived[4], char pass[4], char ans[1]) { bool correct; if(passReceived[0]==pass[0] && passReceived[1]==pass[1] && passReceived[2]==pass[2] && passReceived[3]==pass[3]){ myled=1; ans[0] = '1'; correct=true; } else { myled2=1; ans[0] = '1'; correct=false; } return correct; } int main() { char inputBuffer[4]; char outputBuffer[4]; char answer[1]; EthernetInterface eth; TCPSocketConnection sock; eth.init("192.168.1.102", "255.255.255.0","192.168.1.100"); eth.connect(); sock.connect("192.168.1.101", 5541); //receive the data sock.receive(inputBuffer,sizeof(inputBuffer)); //1010 to outputbuffer passTeclado(outputBuffer); //compare outputbuffer and inputbuffer, give an answer compare(inputBuffer,outputBuffer,answer); //send the answer sock.send(answer,sizeof(answer)); //close all sock.close(); eth.disconnect(); return 0; }
In my pc code (java) in my main ( and for dont copy everything):
//Conection With Port TCP server = new TCP(5541); //Create a new server server.newConnection(); //Is connected? System.out.print(server.connect()+"\n"); //Send to mbed server.sendstring("1111"); //Read the received and print String read; read = server.receive(); System.out.print(read+"\n"); //close all server.fechar(); server.closeServ(); System.exit(0);
I don't know why but when i try the newConnection:
public void newConnection() throws IOException{ sock = serverSocket.accept(); inputStream = new DataInputStream(sock.getInputStream()); outputStream = new DataOutputStream(sock.getOutputStream()); }
The code don't do anything, stopped in line "sock = serverSocket.accept();"..
I configured my ethernet cable(ip,gateway..) with the specifications on my mbed code, and i think ethernetcable is correctly turn on, on my mbed:
green/white -> TD+
green -> TD-
orange/white -> RD+
orange -> RD-
Can someone know what is this error?
PS: Sorry about some error on my english.