# socket client- server windows<->linux



## hphi (21. November 2007)

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:

```
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


```
#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


----------



## hphi (22. November 2007)

*Problem gelöst: socket client- server windows<->linux*

Die Sockets am Windows - Client wurden nicht abgebaut.

Der Client - Code sieht jetzt so aus:


```
int			nError  =  0;
	int			i		=  0;
	CString		csIp	= static_cast<CBpApp*>(AfxGetApp())->m_pszIp;

	WSADATA		wsaData;
	if ( WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR )
	{
		nError	= -1;
		SetEvent(m_hStopLiveThread);
	}

	while ( nError == 0 )
	{
		SOCKET sock  = -1;
		if ( (sock = 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);
		for ( int iii = 0; iii < 256; iii++ )
			if ( ( connect(sock, (SOCKADDR*)&service, sizeof(service) ) )  != SOCKET_ERROR )
			{
				break;
			}
			else
			{
				Sleep(10);
			}

		if ( iii == 256 )
		{
			nError = -3;
			SetEvent(m_hStopLiveThread);
		}
		if ( nError == 0 )
		{
			char buf[200];

			sprintf(buf, "%d", i++);
			int bytes = send(sock, buf, strlen(buf), 0);

			if ( bytes == SOCKET_ERROR )
			{
				SetEvent(m_hStopLiveThread);
				nError = -4;
			}
			int count;
			do 
			{
				count = recv( sock, buf, sizeof(buf), 0);
			}
			while (count > 0);

			while ( shutdown(sock, SD_BOTH) )
				Sleep(10);
			while ( closesocket(sock) )
				Sleep(10);
			sock = -1;
		}
		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;
```


----------

