currently i am creating a project with LAN based connection. Please show me some sample codes
for the winsock control in Visual Basic 6
kreig12 0 Newbie Poster
AndreRet 526 Senior Poster
Attached plain and basic code. This will set you on the correct path.
Please submit if this helped or if answered.
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with zip files.
omoridi 0 Junior Poster in Training
technology -5 Light Poster
#include <iostream.h>
#include <winsock2.h>
#include <windows.h>
//#include <string>
#include <stdio.h>
#include <string.h>
//using namespace std;
#define MAX_LENGTH 1024
#pragma argsused
char *fgets(char *line, int maxline, FILE *fp);
int fputs(char *line, FILE *fp);
int getline(char *line, int max);
void main()
{
int s_len, r_len;
int skt_Smtp;
int success;
struct sockaddr_in st_Sockaddr;
char recv_Buf[MAX_LENGTH];
char send_Buf[MAX_LENGTH];
char abuf[MAX_LENGTH] = "";;
char bbuf[MAX_LENGTH] = "";;
//using namespace std;
//string my_string;
char text[1024] = "";
//Initialize Sockets
WSADATA wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);
skt_Smtp = socket(AF_INET,SOCK_STREAM,0);
if (skt_Smtp < 0)
{
cout<< "Error Creating Socket"<<endl;
return;
}
else
{
st_Sockaddr.sin_family = AF_INET;
st_Sockaddr.sin_port = htons(25);
//Get the IP address and initialize the structure
st_Sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
success = connect(skt_Smtp,(struct sockaddr *) &st_Sockaddr,sizeof(st_Sockaddr));
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Say Hello to the domain
strcpy(send_Buf,"HELO [email]jameshernon123@oxymoron.homeip.net[/email]\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Send from address
strcpy(send_Buf , "MAIL FROM: <jahernon@gmail.com>\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
//Send RCPT address
//strcpy(send_Buf,"RCPT TO: <jameshernon231@btinternet.com>\r\n");
cout << "RCPT TO:";
//strcpy(text,"RCPT TO:");
strcat(bbuf,"RCPT TO:<");
getline(text,255);
text[strlen(text)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */
strcat(bbuf, text );
//cout << text;
strcat(bbuf,">");
strcpy(send_Buf,bbuf);
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
cout<< send_Buf<<endl;
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
// Send DATA
strcpy(send_Buf,"DATA\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
// Send DATA
//strcpy(send_Buf,"Subject:new subject\r\n\r\n\r\n");
cout << "Enter Subject:";
strcpy(text,"Subject:");
getline(&text[8],255);
int t;
//t=sizeof(text);
t = strlen(text);
strcpy(&text[t],"\r\n\r\n\r\n");
//** printf("\nSubject:%s",text);
strcpy(send_Buf,text);
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
//Send body
cout << "Enter Message:";
getline(text,1024);
int q;
q=sizeof(text);
strcpy(&text[q],"\r\n.\r\n");
//** printf("\nText:%s",text);
strcpy(send_Buf, text );
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n.\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Send QUIT
strcpy(send_Buf,"QUIT.\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
closesocket(skt_Smtp);
}
WSACleanup();
}
/* fgets: get at most n chars from iop */
char *fgets(char *s, int n, FILE *iop)
{
register int c;
register char *cs;
cs = s;
while (--n > 0 && (c = getc(iop)) != EOF)
if ((*cs++ = c) == '\n')
break;
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}
/* fputs: put string s on file iop */
int fputs(char *s, FILE *iop)
{
int c;
while (c = *s++)
putc(c, iop);
return ferror(iop) ? EOF : 0;
}
int getline(char *line, int max)
{
if (fgets(line, max, stdin) == NULL)
return 0;
else
return strlen(line);
}
Edited by Nick Evan because: Fixed formatting
technology -5 Light Poster
forgot to embed the code example in the code tags.
#include <iostream.h>
#include <winsock2.h>
#include <windows.h>
//#include <string>
#include <stdio.h>
#include <string.h>
//using namespace std;
#define MAX_LENGTH 1024
#pragma argsused
char *fgets(char *line, int maxline, FILE *fp);
int fputs(char *line, FILE *fp);
int getline(char *line, int max);
void main()
{
int s_len, r_len;
int skt_Smtp;
int success;
struct sockaddr_in st_Sockaddr;
char recv_Buf[MAX_LENGTH];
char send_Buf[MAX_LENGTH];
char abuf[MAX_LENGTH] = "";;
char bbuf[MAX_LENGTH] = "";;
//using namespace std;
//string my_string;
char text[1024] = "";
//Initialize Sockets
WSADATA wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);
skt_Smtp = socket(AF_INET,SOCK_STREAM,0);
if (skt_Smtp < 0)
{
cout<< "Error Creating Socket"<<endl;
return;
}
else
{
st_Sockaddr.sin_family = AF_INET;
st_Sockaddr.sin_port = htons(25);
//Get the IP address and initialize the structure
st_Sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
success = connect(skt_Smtp,(struct sockaddr *) &st_Sockaddr,sizeof(st_Sockaddr));
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Say Hello to the domain
strcpy(send_Buf,"HELO someone@someserver.net\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Send from address
strcpy(send_Buf , "MAIL FROM: <someone@gmail.com>\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
//Send RCPT address
//strcpy(send_Buf,"RCPT TO: <someone@somewhere.com>\r\n");
cout << "RCPT TO:";
//strcpy(text,"RCPT TO:");
strcat(bbuf,"RCPT TO:<");
getline(text,255);
text[strlen(text)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */
strcat(bbuf, text );
//cout << text;
strcat(bbuf,">");
strcpy(send_Buf,bbuf);
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
cout<< send_Buf<<endl;
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
// Send DATA
strcpy(send_Buf,"DATA\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
//cout<< recv_Buf<<endl;
// Send DATA
//strcpy(send_Buf,"Subject:new subject\r\n\r\n\r\n");
cout << "Enter Subject:";
strcpy(text,"Subject:");
getline(&text[8],255);
int t;
//t=sizeof(text);
t = strlen(text);
strcpy(&text[t],"\r\n\r\n\r\n");
//** printf("\nSubject:%s",text);
strcpy(send_Buf,text);
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
//Send body
cout << "Enter Message:";
getline(text,1024);
int q;
q=sizeof(text);
strcpy(&text[q],"\r\n.\r\n");
//** printf("\nText:%s",text);
strcpy(send_Buf, text );
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
strcpy(send_Buf,"\r\n.\r\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
//Send QUIT
strcpy(send_Buf,"QUIT.\n");
s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';
cout<< recv_Buf<<endl;
closesocket(skt_Smtp);
}
WSACleanup();
}
/* fgets: get at most n chars from iop */
char *fgets(char *s, int n, FILE *iop)
{
register int c;
register char *cs;
cs = s;
while (--n > 0 && (c = getc(iop)) != EOF)
if ((*cs++ = c) == '\n')
break;
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}
/* fputs: put string s on file iop */
int fputs(char *s, FILE *iop)
{
int c;
while (c = *s++)
putc(c, iop);
return ferror(iop) ? EOF : 0;
}
int getline(char *line, int max)
{
if (fgets(line, max, stdin) == NULL)
return 0;
else
return strlen(line);
}
kinwang2009 7 Posting Whiz in Training
What type of program you are trying to develop? Are you trying to develop a simple chat program using Winsock? or trying to developed other online programs.
thanks
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.