![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
any help in sockets (i can`t enter the server)
#include <winsock.h>
#include <iostream.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define DEST_IP "194.68.45.50" #define DEST_PORT 6666 void main() { WSADATA wsaData; char buf[2000]; int sockfd; int len, bytes_recv; char msg[2000] = "NICK omakhjfg"; char mss[2000] = "USER bot_lo 8 * :C IRC Hacks Robot"; len=sockfd=0; struct sockaddr_in dest_addr; if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { cout<<"WSAStartup failed.\n"; exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); dest_addr.sin_family = AF_INET; // host byte order dest_addr.sin_port = htons(DEST_PORT); // short, network byte order dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)); bytes_recv = recv(sockfd, buf, 1999, 0); buf[bytes_recv] = '\0'; cout<<buf<<"\n"; len = strlen(msg); send(sockfd, msg, 1999, 0); send(sockfd, "\r", 10, 0); send(sockfd, mss, 1999, 0); send(sockfd, "\r", 10, 0); buf[bytes_recv] = '\0'; cout<<buf<<"\n"; closesocket(sockfd); WSACleanup(); } |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Oh my poor poor eyes,
A few points: +You Need Code Tags +Never use void main, use "int main()" and return 0 or 1. +You are using deprecated headers. eg. iostream.h, it should be #include <iostream> You've showed us some code, and? You haven't even asked a question.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#3 | |
|
Professional Programmer
|
It looks like you found the oldest winsock code you could find and just strangled it. Why are you using winsock instead of winsock2?
If I was using your code, I would change it to look like so(up to the connection)... #include <winsock2.h>
#include <iostream.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define DEST_IP "194.68.45.50"
#define DEST_PORT 6666
int main()
{
WSADATA wsaData;
char buf[2000];
int sockfd;
int len, bytes_recv;
char msg[2000] = "NICK omakhjfg";
char mss[2000] = "USER bot_lo 8 * :C IRC Hacks Robot";
len=sockfd=0;
struct sockaddr_in dest_addr;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout<<"WSAStartup failed.\n";
exit(1);
}
if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
/* Make sure you have an error message so you know if this is the problem */
cout << "socket initialization failed";
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
memset(&(Remote_address.sin_zero), '\0', 8); // Zero out the rest of the struct
if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1)
/* Make sure you have an error message so you know if this is the problem */
cout << "connection failed";EDIT: I lied, I would actually change all the code, but that's your job.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
the problem that i can`t logon into irc.dal.net try it urself
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Works for me.
#include <winsock.h>
#include <iostream>
#include <string>
#define DEST_IP "194.68.45.50"
#define DEST_PORT 6666
using namespace std;
int main()
{
WSADATA wsaData;
SOCKET sockfd;
int len = 0, bytes_recv = 0;
char buf[2048];
string msg = "NICK omgrolfcopterlol";
string mss = "USER bot_lo 8 * :C IRC Hacks Robot";
struct sockaddr_in dest_addr;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
cout << "WSAStartup failed." << endl;
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(DEST_PORT);
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
bytes_recv = recv(sockfd, buf, 1999, 0);
buf[bytes_recv] = '\0';
cout << buf << endl;
len = msg.length();
send(sockfd, msg.c_str(), 1999, 0);
send(sockfd, "\r", 10, 0);
send(sockfd, mss.c_str(), 1999, 0);
send(sockfd, "\r", 10, 0);
buf[bytes_recv] = '\0';
cout << buf << endl;
closesocket(sockfd);
WSACleanup();
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates Last edited by nnxion; Apr 18th, 2006 at 2:29 PM. Reason: Using C++ strings |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
but it gives first two lines only which is 1) cheking identify 2)found your hostname
i want to send back my nickname but i can`t |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
You are quiting, it doesn't do much more than that. I didn't look very closely at what it was supposed to do. Rather fixed a bit of your deprecated code.
You do work according to this right? Edit: you might also want to check this.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
i know some of the irc rfc how can i send enter to the socket
|
|
|
|
|
|
#9 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
|
|
#10 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
i mean send return to the server
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|