Hallo liebe Leute,
ich experimentiere schon eine ganze Weile mit Sockets herum.
Der Client (Windows XP) schickt Daten, und der Server (Debian
Etch - Linux) soll diese Daten ausgeben. Das funktioniert auch
schon ansatzweise. Nur scheint die Leitung nach dem 1020ten
Datenpaket verstopft zu sein, d.h. es kommen keine Daten mehr durch. Der Server bekommt ein timeout, und der Client-Thread beendet sich ordnungsgemäß.
Auch habe ich festgestellt, daß der Client sich jedesmal neu connecten muß.
Hier ist der Code:
Windows:
Linux
Wenn jemand eine Idee hat wäre ich sehr dankbar für Tips, da ich schon eine Woche an dem Problem arbeite.
Viele Grüße
hphi
ich experimentiere schon eine ganze Weile mit Sockets herum.
Der Client (Windows XP) schickt Daten, und der Server (Debian
Etch - Linux) soll diese Daten ausgeben. Das funktioniert auch
schon ansatzweise. Nur scheint die Leitung nach dem 1020ten
Datenpaket verstopft zu sein, d.h. es kommen keine Daten mehr durch. Der Server bekommt ein timeout, und der Client-Thread beendet sich ordnungsgemäß.
Auch habe ich festgestellt, daß der Client sich jedesmal neu connecten muß.
Hier ist der Code:
Windows:
Code:
int nError = 0;
int i = 0;
CString csIp = static_cast<CBpApp*>(AfxGetApp())->m_pszIp;
while ( nError == 0 )
{
SOCKET s = -1;
WSADATA wsaData;
if ( WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR )
{
nError = -1;
SetEvent(m_hStopLiveThread);
}
if ( (s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
{
nError = -2;
SetEvent(m_hStopLiveThread);
}
sockaddr_in service;
memset(&service, 0, sizeof(service));
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(csIp);
service.sin_port = htons(60180);
if ( connect(s, (SOCKADDR*)&service, sizeof(service) ) == SOCKET_ERROR )
{
nError = -3;
SetEvent(m_hStopLiveThread);
}
if ( nError == 0 )
{
char buf[200];
sprintf(buf, "%d", i++);
int bytes = send(s, buf, strlen(buf), 0);
if ( bytes == SOCKET_ERROR )
{
SetEvent(m_hStopLiveThread);
nError = -4;
}
}
//closesocket(s);
if ( WaitForSingleObject(m_hStopLiveThread, 0) == WAIT_OBJECT_0 )
{
SetEvent(m_hLiveThreadStopped);
break;
}
WSACleanup();
}
switch ( nError )
{
case -1 :
break;
case -2 :
break;
case -3 :
break;
case -4 :
break;
}
if ( m_btnLiveAnnouncement.m_hWnd )
m_btnLiveAnnouncement.setRunning();
return nError;
Linux
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
int socketstrean(int, struct sockaddr_in*);
int main()
{
int fddata = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in data;
data.sin_family = AF_INET;
data.sin_addr.s_addr = INADDR_ANY;
data.sin_port = htons(60180);
bind(fddata, (struct sockaddr*)&data, sizeof(data));
listen(fddata, 3);
switch ( socketstrean(fddata, &data) )
{
case -1 :
puts("Error");
break;
case 1 :
puts("Timeout");
break;
}
close(fddata);
return 0;
}
int socketstrean(int fddata, struct sockaddr_in* data)
{
fcntl(fddata, F_SETFL, O_NONBLOCK);
fd_set rfds;
char databuf[1024];
struct timeval timeout;
timeout.tv_usec = 1000;
timeout.tv_sec = 20;
while ( true )
{
FD_ZERO(&rfds);
FD_SET(fddata, &rfds);
// puts("waiting...");
int total = select(fddata + 1, &rfds, NULL, NULL, &timeout);
switch ( total )
{
case 0 :
puts("total=0");
return 1;
case -1:
puts("total=-1");
return -1;
}
if ( FD_ISSET(fddata, &rfds) )
{
socklen_t datalen = sizeof(*data);
int s;
if ( ( s = accept(fddata, (struct sockaddr *)data, &datalen) ) >= 0 )
{
int datarecv = recv(s, databuf, sizeof(databuf), 0);
if ( datarecv > 0 )
{
databuf[datarecv] = '\0';
puts(databuf);
fflush(stdout);
}
}
}
/*
else
{
puts("no data");
fflush(stdout);
}
*/
}
return 0;
}
Wenn jemand eine Idee hat wäre ich sehr dankbar für Tips, da ich schon eine Woche an dem Problem arbeite.
Viele Grüße
hphi