|
|
|
|
||||||
| comp.protocols.tcp-ip TCP and IP network protocols. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I'm having trouble with this class...
I don't seem to be transmitting or receiving data... I want to try sending and receiving udp packets... I have client and a server (so to speak) the client send hello and then expects the receiver to echo it back. updclient.cpp (Just general purpose UPD routines) udpclient::udpclient(void) { sd = -1; } void udpclient::create(char *hostname, int portno) { if ((hp = gethostbyname(hostname)) == NULL) { addr.sin_addr.s_addr = inet_addr(hostname); hp = gethostbyaddr((char *)&addr.sin_addr.s_addr, sizeof(addr.sin_addr.s_addr), AF_INET); } printf("----UDPServer1 running at host NAME: %s\n", hp->h_name); memcpy(&server.sin_addr, hp->h_addr, hp->h_length); printf("(UDPServer1 INET ADDRESS is: %s )\n", inet_ntoa(server.sin_addr)); /*** Construct name of socket to send to. ***/ server.sin_family = AF_INET; server.sin_port = htons(portno); /*** Create socket on which to send and receive ***/ sd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sd < 0) throw("Can't create socket."); } void udpclient::send(const char *buf, int n) { sendto(sd, buf, n, 0, (struct sockaddr *) &server, sizeof(server)); } int udpclient::receive(char *rbuf, int rbufsize) { socklen_t fromlen = sizeof(from); int rec = recvfrom(sd, rbuf, rbufsize, 0, (struct sockaddr *) &from, &fromlen); return(rec); } void udpclient::closeSock(void) { close(sd); sd = -1; } udpclient::~udpclient() { if (sd != -1) close(sd); } testclient.cpp #include <iostream> #include "udpclient.h" int main (int argc, char * const argv[]) { udpclient c; char buffer[7]; c.create("192.168.1.100", 5000); for (int i=0; i < 10; ++i) { c.send("hello", 6); c.receive(buffer, 6); } c.closeSock(); } testserver.cpp #include <iostream> #include "udpclient.h" int main (int argc, char * const argv[]) { udpclient c; char buffer[7]; c.create("192.168.1.100", 5000); for (int i=0; i < 10; ++i) { c.receive(buffer, 6); c.send(buffer, 6); } c.closeSock(); } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
SpreadTooThin wrote:
> I'm having trouble with this class... > I don't seem to be transmitting or receiving data... > I want to try sending and receiving udp packets... > I have client and a server (so to speak) > the client send hello and then expects the receiver to echo it back. If there were any error checking in that code you could likely answer your own question. -- Phil Frisbie, Jr. Hawk Software http://www.hawksoft.com |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Phil Frisbie, Jr. wrote: > SpreadTooThin wrote: > > I'm having trouble with this class... > > I don't seem to be transmitting or receiving data... > > I want to try sending and receiving udp packets... > > I have client and a server (so to speak) > > the client send hello and then expects the receiver to echo it back. > > If there were any error checking in that code you could likely answer > your own question. > > -- > Phil Frisbie, Jr. > Hawk Software > http://www.hawksoft.com Shall I post the same code with error checking? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
SpreadTooThin wrote:
> Phil Frisbie, Jr. wrote: > >>SpreadTooThin wrote: >> >>>I'm having trouble with this class... >>>I don't seem to be transmitting or receiving data... >>>I want to try sending and receiving udp packets... >>>I have client and a server (so to speak) >>>the client send hello and then expects the receiver to echo it back. >> >>If there were any error checking in that code you could likely answer >>your own question. > > Shall I post the same code with error checking? No. You need to run the code with error checking and if you do not understand the error code you get THEN post your source code AND the error code. -- Phil Frisbie, Jr. Hawk Software http://www.hawksoft.com |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Phil Frisbie, Jr. wrote: > SpreadTooThin wrote: > > > Phil Frisbie, Jr. wrote: > > > >>SpreadTooThin wrote: > >> > >>>I'm having trouble with this class... > >>>I don't seem to be transmitting or receiving data... > >>>I want to try sending and receiving udp packets... > >>>I have client and a server (so to speak) > >>>the client send hello and then expects the receiver to echo it back. > >> > >>If there were any error checking in that code you could likely answer > >>your own question. > > > > Shall I post the same code with error checking? > > No. You need to run the code with error checking and if you do not > understand the error code you get THEN post your source code AND the > error code. > > I am not throwing any exceptions or errors... I have ported the code to windows and am testing between MAC OS X and windows. Client and server work perfectly underwindows... The udp server however is behaving oddly on mac os x. -- > Phil Frisbie, Jr. > Hawk Software > http://www.hawksoft.com |
|
![]() |
| Outils de la discussion | |
|
|