|
You last visited: Today at 05:30
Advertisement
Problem mit Sockets
Discussion on Problem mit Sockets within the C/C++ forum part of the Coders Den category.
02/27/2011, 20:09
|
#1
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
Problem mit Sockets
Hey ho, ich habe gestern angefangen ne kleine Socketklasse zu schreiben, habe mir dazu das c-worker tut genommen.
Es gibt ein kleines Problem mit dem Port...
Ich zeige euch mal mein kleines Projekt:
socket.cpp:
Code:
// Class automatically generated by Dev-C++ New Class wizard
#include "socket.h" // class's header file
// class constructor
Socket::Socket(char IpAddress[],u_short Port)
{
this->Port = Port;
strcpy(this->IpAddress,IpAddress);
}
bool Socket::Connect(){
if(StartWinsock() == 0){
SOCKET s = socket(AF_INET,SOCK_STREAM,0);
if (s != INVALID_SOCKET){
SOCKADDR_IN addr;
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons((u_short)this->Port);
addr.sin_addr.s_addr = inet_addr(this->IpAddress);
if (connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR))!= SOCKET_ERROR){
return true;
}else{
MessageBox(0,"Nicht connectet","",0);
return false;
//Fehler
}
}else{
MessageBox(0,"Socket nicht erstellt","",0);
return false;
//Fehler
}
}else{
MessageBox(0,"Winsock nicht gestartet","",0);
return false;
//Fehler
}
}
int Socket::StartWinsock(){
WSADATA wsa;
return WSAStartup(MAKEWORD(2,0),&wsa);
}
// class destructor
Socket::~Socket()
{
// insert your code here
}
socket.h:
Code:
// Class automatically generated by Dev-C++ New Class wizard
#ifndef SOCKET_H
#define SOCKET_H
#include <winsock.h>
#include <windows.h>
// Genereller Socket
class Socket
{
public:
// class constructor
Socket(char IpAddress[],u_short Port);
bool Connect();
// class destructor
~Socket();
private:
int StartWinsock();
char IpAddress[];
u_short Port;
};
#endif // SOCKET_H
in meiner main dann
Code:
Socket s("127.0.0.1",1889);
if(s.Connect() == true){
MessageBox(0,"Verbindung hergestellt","",0);
}
Das Problem liegt anscheinent hier:
addr.sin_port = htons((u_short)this->Port); (ich weiß, dass der carst schwachsinnig ist)
Wenn ich den Port normal dort eintrage, als Zahl, dann gibt es keine Probleme, die Verbindung wird aufgenommen.
Ich verwende devc++...
Ich schreibe noch nicht so lange in c++, würde mich freuen, wenn mir jemand sagen könnte, woran es liegt und wie ich es beheben kann.
|
|
|
02/27/2011, 22:26
|
#2
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Gibts auch eine genaue Fehlermeldung ?
Überhaupt keine Fehlerbehandlung ist auch keine Option 
Wenn der Cast sinnlos ist warum steht er dann noch da ?
|
|
|
02/27/2011, 22:41
|
#3
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
if (connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR)) != SOCKET_ERROR
diese Bedingung ist nicht erfüllt, der socket connectet nicht.
ich hatte den Port ursprünglich als int deklariert und dann zum u_short gecastet, als mir auffiel, dass die funktion einen u_short als parameter erwartet.
hat aber alles nicht geholfen.
Quote:
|
Überhaupt keine Fehlerbehandlung ist auch keine Option
|
sehe in dem satz keinen sinn, ich überprüfe doch nach jedem schritt, ob ein fehler aufgetreten ist.
Wier schon gesagt, addr.sin_port = htons(1889); fuktioniert wunderbar.
|
|
|
02/27/2011, 22:45
|
#4
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Schau mal welchen Error Code WSAGetLastError() ausspuckt
also um nochmal sicher zu gehen kompillieren kannst du es nur
beim Connecten läufts schief
|
|
|
02/27/2011, 22:57
|
#5
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Mach das u_short mal public vllt hilft das was
|
|
|
02/27/2011, 23:05
|
#6
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
10061,WSAECONNREFUSED
No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.
Es kann keine Verbindung hergestellt werden, weil auf dem Port keine Serveranwendung läuft. Ich habe allerdings eine Serveranwendung auf selbigem Port laufen.
Danke schonmal für deine Bemühungen.
Edit: daran kann es nicht liegen, da char[] IpAddress ja ebenfalls private ist...
habs versucht, funktioniert natürlich auch nicht.
|
|
|
02/27/2011, 23:08
|
#7
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Kein Ding müsste eigtl an der server Anwendung liegen
kann ich vllt die Anwendung mal sehen ?
|
|
|
02/27/2011, 23:16
|
#8
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
es liegt definitiv nicht an der serveranwendung. sobald ich der htons() Funktion den Port direkt übergebe:
htons(1889)
funktioniert es wunderbar, die Serveranwendung zeigt dann auch eine Verbindung an.
Das Problem wird clientseitig sein.
Die Serverapp ist in .net geschrieben, aber das tut nicht zur sache.
Ich habe mir mal this->Port ausgeben lassen, da kommt 12849 raus oO, obwohl ich im Konstruktior den Wert verändert habe.
|
|
|
02/27/2011, 23:21
|
#9
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Komisch schreib einfach mal ohne den this Zeiger
du solltest auch WSACleanup() benutzen
|
|
|
02/27/2011, 23:27
|
#10
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
habe ich schon versucht.
ist wirklich crazy, wenn ich
this->Port = 1889;
eine zeile vor
addr.sin_port = htons(this->Port);
schreibe funktioniert es nicht, aber wenn ich mir dann den Inhalt von this->Port ausgeben lasse, kommt auf einmal doch 1889 raus.
|
|
|
02/27/2011, 23:31
|
#11
|
elite*gold: 0
Join Date: Oct 2010
Posts: 381
Received Thanks: 210
|
Schön das Programmiersprachen IMMER logisch sind
|
|
|
02/28/2011, 17:36
|
#12
|
elite*gold: 77
Join Date: May 2008
Posts: 5,430
Received Thanks: 5,878
|
Ich komme mit dem "this->Port=Port;" ganz erhlich nicht ganz klar...
Es kann sein, dass ich mich jetzt total irre, aber so wäre es doch um einiges einfacher/schöner:
PHP Code:
Socket::Socket(char IpAddress[],u_short Port1)
{
Port = Port1;
strcpy(this->IpAddress,IpAddress);
}
Ähnliches gilt für "this->IpAddress".
Desweiteren verstehe ich nicht was genau "u_short" soll?
Warum nimmst du denn nicht einfach "unsigned short"?
Das ist sogar standartmäßig implementiert und damit sollten die meisten Funktionen klar kommen. Wie genau "u_short" definiert ist weiß ich nicht, aber ich finde man sollte den Standart einhalten, wenn möglich.
|
|
|
02/28/2011, 21:03
|
#13
|
elite*gold: 0
Join Date: May 2008
Posts: 132
Received Thanks: 120
|
die funktion ist mit einem u_short in der winsock2.h so definiert.
Port = Port1; habe ich natürlich auch schon ausprobiert xD funktioniert ebenfalls nicht.
ich schnappe mir die werte jetzt einfach in der connect funktion, das geht soweit.
|
|
|
03/03/2011, 22:31
|
#14
|
elite*gold: 0
Join Date: Jul 2010
Posts: 56
Received Thanks: 12
|
hi
in der socket.h Datei deklarierst du ja
Quote:
//socket.h
char IpAddress[];
|
und in der socket.cpp Datei kopierst du
Quote:
//socket.cpp
strcpy(this->IpAddress,IpAddress);
|
aber du initialisiert, so weit ich das gesehen habe, keinen Speicher für den "IpAddress" Pointer.
Versuch mal mit "new" Speicher zuzuweisen ;-)
|
|
|
03/04/2011, 00:38
|
#15
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,909
Received Thanks: 25,409
|
Oder nimm einen festen Wert als Arraygröße.
Eine IP kann ja nicht länger als "***.***.***.***" werden.
|
|
|
 |
|
Similar Threads
|
sockets
12/17/2006 - Conquer Online 2 - 7 Replies
To Mr.Experts out there, stop misleading the noobs.... Don't make these poor fellas go around wasting their time getting their gears to 0 dura, waking up at some particular time standing at coord x,y to go spamming
THIS IS PLAIN EVIL, my cousin is only 12 and I know alot of kids around his age are not intelligent enough to discern from facts and theories...
1.Stop arguing whether if it existed or not....It doesn't matter really!!
2.Before TQ's recent patches...this is how everyone I've...
|
about sockets
08/12/2006 - Conquer Online 2 - 2 Replies
well i talked today with a former co player , and i asked her about how to make sockets and this stuff and she told me that coords dont matter or time or anything, and that some peoples on some server have a program or something wich tells u when to go spam mets
i dont know now if this is true, and this is what i wana find out
|
sockets
05/15/2006 - Conquer Online 2 - 20 Replies
guys im hearing from multiple sorces that there are specific places you can go to hunt in CO that the monsters actually drop socketed items, aside from wepons
i was told that in the map past metzone thoes creatures will drop socketed items, always refined or better, Nightdevils and Evil Ghosts are pure magic, so the suggested level to go is 120 +, and if your not a tao get some earrings
if anyone knows anything about this i guess post or direct me to one previous if its already been made...
|
Sockets in CO?
03/16/2006 - Conquer Online 2 - 14 Replies
I was reading through the socket thread on the other forum and such... I know a few people who are 100 pct on making 1 socket and around 90 pct on the 2nd socket... So I am wondering if there is a way to make it happen without a chance of failure? I know that the Conquer Online Beta Client (the old old one) you can make sockets without worry on anything with that client... Does anyone know where to get it or know someone who'd upload it to a server? Or just knows a fool proof way to...
|
sockets
05/24/2005 - Conquer Online 2 - 158 Replies
"I have currently working .dat modded file of monster to make Meteor Dove huge to make it easier to see. This one has been out for a while. The .dat modded file to make people fly and also the extensive trick to d/c people. Also have the trick to socket armor as easily as you were making a socket a weapon. This trick you need the exploit and another trick goes with it. All the hacks are currently working. I am just waiting for the cue to release them. I have screenshots of them working as well....
|
All times are GMT +1. The time now is 05:30.
|
|