Ik wil TCP/IP communicatie opzetten, maar het lukt me niet om socket messages één voor één uit te lezen. Bij UDP lukte dat prima. De code van de server:
Als ik bij UDP de getallen 1-10 in 10 berichten ontvang kan ik die getallen "los" uitlezen. Bij TCP krijg ontvang ik eerst netjes "1" waarna ik bij de tweede keer uitlezen "2345678910" in mijn ontvangstbuffer lees. Een oplossing die ik zelf had bedacht is een separatieteken in te voeren bij verzending, waardoor ik messages alsnog kan onderscheiden,maar dat wil ik natuurlijk eigenlijk niet. Iemand een suggestie?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define buffermax 250
#define messagemax 16
#pragma comment(lib, "ws2_32.lib") // to get rid of linker errors
char EOT=0x04;
char kansloos;
int iMode;
int count=0;
int ret_chars,j,i=0;
int client_sock;
int n=0;
int error=0;
int Errorcode;
char buff[buffermax];
char number[10];
int huidig;
int laatste=1;
int teller=0;
int aantal;
int tcp_sock;
int bindok;
struct sockaddr_in data;
/* Van Gabriel ivm gebruik ws2_32.dll */
int err ;
WORD wVersionRequested = MAKEWORD( 2, 2 );
WSADATA wsaData;
int main(int argc, char *argv[])
{
/* invoer aantal verzonden pakketten */
/* Initialize ws2_32.dll */
printf("\nInitializing ws2_32.dll ...\n");
err = WSAStartup( wVersionRequested, &wsaData );
if (err != 0)
{
/* Could not find usable WinSock.DLL */
printf ("\nerr: %d\n", err);
exit(1);
}
//ready to open socket
printf("press any key to continue\nwaiting");
getch();
printf("\n");
//Open Socket
tcp_sock = socket( AF_INET, SOCK_STREAM, 0);
if(tcp_sock < 0)
{
printf("Could not open socket %d\n\r", tcp_sock);
getch();
return tcp_sock;
}
//Structure klaarzetten
if(tcp_sock)
{
memset( &data, 0, sizeof( data ) ); // clear out our structure
data.sin_family = AF_INET; // Set Internet address Family
data.sin_addr.s_addr = htonl( NULL ); // Client address will be filled
// in automatically
data.sin_port = htons(1010); // This sets the TCP port number
}
//bind
bindok = bind( tcp_sock, ( struct sockaddr *)&data, sizeof( data ) ); // call to bind
if( bindok < 0 )
{
printf("could not bind socket to file descriptor\n\r");
}
//client connects
//accept
listen( tcp_sock, 10); // listen for 10 clients Max
tcp_sock = accept( tcp_sock, NULL, NULL ); // call to accept
//receive
memset( buff, 0, sizeof( buff ) );
while (1)
{
ret_chars = recv( tcp_sock, buff, sizeof(buff), 0);
// printf("%s\n", buff);
if( ret_chars > 0 )
{
//verwerk data
//code voor de leesbaarheid weggelaten
//ik ben gedwongen om hier met een seperatieteken te werken om messages te
//kunnen onderscheiden!
}
else if( ret_chars < 0 )
{
printf("error recv'ing data\n");
Errorcode=WSAGetLastError();
if (Errorcode==10054)
{
printf("Connection failed, client down\n");
break;
}
else printf("Errorcode = %d\n", Errorcode);
}
else if( ret_chars == 0 )
{
printf("client session terminated\n");
break;
}
memset( buff, 0, sizeof( buff ) );
}
printf("%d packages received\n", aantal);
printf("press any key to continue\n");
getch();
//close socket
closesocket(tcp_sock);
} |
Als ik bij UDP de getallen 1-10 in 10 berichten ontvang kan ik die getallen "los" uitlezen. Bij TCP krijg ontvang ik eerst netjes "1" waarna ik bij de tweede keer uitlezen "2345678910" in mijn ontvangstbuffer lees. Een oplossing die ik zelf had bedacht is een separatieteken in te voeren bij verzending, waardoor ik messages alsnog kan onderscheiden,maar dat wil ik natuurlijk eigenlijk niet. Iemand een suggestie?