ERLEDIGT
JA
JA
ANTWORTEN
5
5
ZUGRIFFE
397
397
EMPFEHLEN
-
30.12.08 22:24 #1
- Registriert seit
- Jun 2008
- Ort
- Nah bei Köln
- Beiträge
- 252
Hallo Leute ich bins wieder

also, hab folgenden Code erstmal:
Code :1 2 3 4 5 6 7
System::Net::Sockets::TcpClient^ client = gcnew System::Net::Sockets::TcpClient("10.0.100.137", 9999); System::String^ blubb = "blubb"; array<Byte>^ data = System::Text::Encoding::ASCII->GetBytes(blubb); System::Net::Sockets::NetworkStream^ stream = client->GetStream(); // Send the message to the connected TcpServer. stream->Write( data, 0, data->Length );
Das wäre mein Code zum Senden eines schlichten "Blubb" an einen Netzwerkserver mit der IP 10.0.100.137 (z.B.) und dem Port 9999. Wenn ich diesen Schnipsel aud einen SDL_net Server anwende erhalte ich hinterher (in dem Konsolenfenster) ein
statt einblubb╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Xdì
Warum? kann mir das einer erklären? Von SDL_net-Client -> SDL_net-Server gehts einwandfrei, nur mit einem .NET Clienten halt net. Ich kann leider net auf einen SDL_net Server verzichten, da ich (ausser ihr schlagt mir eine auf Linux passende variante vor) den Server auf Linux installieren möchte.blubb
Vielen Dank im Voraus!
WR
-
17.01.09 13:54 #2
- Registriert seit
- Jun 2008
- Ort
- Nah bei Köln
- Beiträge
- 252
*schubs* Kann mir da wirklich keiner helfen? Suche immernoch verzweifelt nach einer Lösung.
-
Hmm probiers mal mit der Socket Klasse und der Send Methode.
http://msdn.microsoft.com/de-de/libr...et(VS.80).aspx
http://msdn.microsoft.com/de-de/libr...nd(VS.80).aspx
-
18.01.09 11:28 #4
- Registriert seit
- Jun 2008
- Ort
- Nah bei Köln
- Beiträge
- 252
Hab schon eine Lösung, danke trotzdem. Hab mit Hile eines Tutorials auf das Linux-Original zugegriffen. Poste euch gleich dann den objektorientierten Code.
So es ist soweit:
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 140 141 142 143 144 145
/* >> SocketHelper Class << Marco Klein, 18.01.2009 11:44 This class gives you the possibility to use linux sockets the easy way. What you have to do is: -> Include SocketHelper.h -> Initialize 'SocketHelper' class -> Call the 'OpenSocket' method -> Call the 'Listen' method -> Then call the 'RecieveData' method. -> To get your message you have to: > Call the 'CopyMessage' method > Then get your char[256] of the class named 'Message' -> If you want to, you can 'Answer' a char* to your client with the Answer method. To close your connection, use "CloseSocket" You have to include the following headers: #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> Have Fun c) 2009 by WorldRacer, http://www.devforyou.de */ #define SH_READY_FOR_LISTENING 1 #define SH_ACCEPT_FAILED 1 #define SH_ERROR_OPENING_SOCKET 1 #define SH_ERROR_BINDING 1 #define SH_ERROR_READING_SOCKET 1 #define SH_ERROR_SENDING_MSG 1 #define SH_NOT_ALLOWED_YET 1 using namespace std; class SocketHelper { public: SocketHelper(int port); int Listen(void); int OpenSocket(void); int RetrieveMessage(void); int Answer(char *data); int CopyMessage(void); char Message[256]; int CloseSocket(void); private: int status; int sockfd, newsockfd, portno, clilen, n; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; }; SocketHelper::SocketHelper(int port){ this->portno = port; this->status = 0; Message[0] = 'N'; Message[1] = 'O'; Message[2] = '_'; Message[3] = 'M'; Message[4] = 'E'; Message[5] = 'S'; Message[6] = 'S'; Message[7] = 'A'; Message[8] = 'G'; Message[9] = 'E'; Message[10] = '_'; Message[11] = 'C'; Message[12] = 'O'; Message[13] = 'P'; Message[14] = 'I'; Message[15] = 'E'; Message[16] = 'D'; Message[17] = '\0'; } int SocketHelper::OpenSocket(void){ this->sockfd = socket(AF_INET, SOCK_STREAM, 0); if (this->sockfd < 0) return 1; bzero((char *) &this->serv_addr, sizeof(this->serv_addr)); this->serv_addr.sin_family = AF_INET; this->serv_addr.sin_port = htons(portno); this->serv_addr.sin_addr.s_addr = INADDR_ANY; if (bind(this->sockfd, (struct sockaddr *) &this->serv_addr, sizeof(this->serv_addr)) < 0){ return 2; } this->status = 1; return 0; } int SocketHelper::Listen(void){ if(this->status != 1){ return 3; } this->status = 2; listen(this->sockfd,5); } int SocketHelper::RetrieveMessage(void){ int error; if(this->status != 2){ return 3; } this->clilen = sizeof(this->cli_addr); this->newsockfd = accept(this->sockfd, (struct sockaddr *) &this->cli_addr, (socklen_t *) &this->clilen); if (this->newsockfd < 0){ return 1; } bzero(this->buffer,255); this->n = read(this->newsockfd,this->buffer,255); if (this->n < 0){ return 2; } return 0; } int stringlength(char *s) { int len=0; while(*s !=0) { len++; s++; } return len; } int SocketHelper::Answer(char *data){ this->n = write(this->newsockfd,data,stringlength(data)); if (this->n < 0) return 1; return 0; } int SocketHelper::CloseSocket(void){ return close(this->sockfd); } int SocketHelper::CopyMessage(void){ int i; for(i=0; i<=255; i++){ this->Message[i] = this->buffer[i]; } return 0; }
Das ist der Code für die Socket-Klasse. Hier nun ein Beispiel:
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
#include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include "include/SocketHelper.h" using namespace std; void error(char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int returnval; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } SocketHelper sh(atoi(argv[1])); returnval = sh.OpenSocket(); if(returnval == SH_ERROR_OPENING_SOCKET ){ error("ERROR opening socket\n"); } if(returnval == SH_ERROR_BINDING ){ error("ERROR on binding\n"); } returnval = sh.Listen(); if(returnval == SH_NOT_ALLOWED_YET){ error("Calling the function is not allowed at the moment."); } returnval = sh.RetrieveMessage(); if(returnval == SH_ACCEPT_FAILED){ error("ERROR on accept"); } if(returnval == SH_ERROR_READING_SOCKET){ error("ERROR reading from socket"); } returnval = sh.CopyMessage(); printf("Here is the message: %s%s",sh.Message,"\n"); returnval = sh.Answer("I got your Message"); if(returnval == SH_ERROR_SENDING_MSG){ error("ERROR writing to socket"); } return 0; }Geändert von WorldRacer (18.01.09 um 12:14 Uhr)
-
18.01.09 13:09 #5
- Registriert seit
- Jun 2008
- Ort
- Nah bei Köln
- Beiträge
- 252
Aber eine Frage hätt ich da noch. Ich hab einen Clienten vom Typ TcpClient der von mehreren Forms genutzt werden soll... Wie stell ich das an, dass alle auf die gleiche Instanz zugreifen?
-
Hi
Das kannst du, in dem du dir ein Klasse schaffst, die die Kommunikation mit dem Server übernimmt und du die Instanz in allen Forms bekannt machst.
Du solltest dann jedoch darauf achten, dass diese Klasse Thread-stabil ist, da du konkurierende Zugriffe haben kannst.Grüße Nico
----------------------
Xing
----------------------
Zitat von Mark Twain (1835-1910)
Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
Ähnliche Themen
-
SSL Sockets?
Von gamp im Forum C/C++Antworten: 2Letzter Beitrag: 19.06.10, 15:46 -
Sockets
Von one6666 im Forum Flash PlattformAntworten: 2Letzter Beitrag: 27.04.10, 21:25 -
Sockets
Von thatIsAFeature im Forum Java GrundlagenAntworten: 2Letzter Beitrag: 23.09.08, 13:39 -
PHP Sockets?
Von pogi im Forum PHPAntworten: 5Letzter Beitrag: 22.09.04, 12:51 -
Sockets
Von Martin Schroeder im Forum C/C++Antworten: 6Letzter Beitrag: 14.07.04, 15:12





Zitieren

Login





