Witam, borykam się z problemem zagadnienia socketów i ogółem wysyłaniem wiadomości do serwera, takiej jakiej on "oczekuje". Problem polega na tym, że jak podam daną wiadomość w funcji send to serwer otrzymuje źle sformatowany xml, a gdy wpiszę ten sam xml "z palca" to wszystko działa poprawnie. Podglądałam też ruch sieci wiresharkiem i mimo iż obie wiadomości wyglądają identycznie to jednak coś knoci się strasznie. Zaznaczam dodatkowo, że umieściłam całość w pętli while, ponieważ planuję stworzyć mini-chat (tak wiem, to może być kiepski pomysł). Chętnie poczytam wszelkie rady i uwagi, bo projekt ten robi się coraz bardziej nerwostratny.
Z góry dziękuję serdecznie :).
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
int main() {
char message[8000], recv_data[8000];
struct sockaddr_in adr;
unsigned int port = 5222;
int sock, bytes_recieved;
fd_set Read0;
struct timeval tv;
sock = socket(PF_INET, SOCK_STREAM, 0);
adr.sin_family = AF_INET;
adr.sin_port = htons(port);
adr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sock, (struct sockaddr*) &adr, sizeof(adr)) < 0) {
printf("Błąd połączenia.\n");
return 1;
}
printf("Połączono\n");
fflush(stdin);
while (1) {
FD_ZERO(&Read0);
if (sock != 0) FD_SET(sock, &Read0);
FD_SET(0, &Read0);
int rv = select(FD_SETSIZE, &Read0, NULL, NULL, &tv);
if (FD_ISSET(0, &Read0)) {
send(sock, "<?xml version=\"1.0\"?> <stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:client\" to=\"localhost\">", 123, 0);
}
if(rv == -1) {
printf("\nError in select\n");
return 1;
}
else if (rv == 0) {
tv.tv_sec = 1;
tv.tv_usec = 0;
}
if( FD_ISSET(sock,&Read0) ) {
bytes_recieved = read(sock, recv_data, 8000);
if (bytes_recieved < 0) printf("\nCouldn't receive data.\n");
else {
recv_data[bytes_recieved] = '\0';
printf("%s", recv_data);
fflush(0);
}
}
}
close(sock);
return 0;
}