How do I use Sockets with C and Windows

No C++, just C, raw sockets.
Working examples only which demonstrate a basic socket connection between 2 endpoints.

This will include:
1. Socket Data Storage Location For Alternative Server Input Command
2. IP Address of connection
3. Port Address of connection
4. Connect command
5. Close Socket command

Attached: Dennis_Ritchie.jpg (220x282, 37.79K)

Other urls found in this thread:

beej.us/guide/bgnet/
docs.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock
twitter.com/SFWRedditVideos

Rip no-one knows.
Or the people that do know are dead...

Ive been working mostly on linux, but if I remember correctly theres almost no difference for simple tasks as described in your list. I think only WSA setup needs to be done. Others can correct me.

>windows
good luck

#include

Provide a working example.

Do your own homework nigger

>raw sockets
This means something else

why? I don't owe you nothing
Do your own research you mongoloid or provide your code and point out why do you think it doesn't work

beej.us/guide/bgnet/
beej is a sperg so the section on translating things to Windows reads as you'd expect, but the guide is still very good

telnet chat utility. Spaces removed
#include
#include
#define PORT_INT 23 // telnet connection port
#define SERVER_ADDR "192.168.1.1"
#define MAXBUF 1024

#define bzero(x,y) memset(x,0,y)

int sockfd;

// read data until get string
void waitforstring(const char *s)
{
int res, pos = 0;
char buffer[MAXBUF];
bzero(buffer, MAXBUF);
while(res = recv(sockfd, buffer + pos, sizeof(buffer) - pos, 0) > 0)
{
if(res > 0)
{
pos += res;
buffer[MAXBUF-1] = 0;
printf("%s", buffer + pos);
if(strstr(buffer, s))
return;
}
}
}

// send string
void sendstring(const char *s)
{
int pos = 0, len = strlen(s);
// Only small messages supported now,
// need to improve this
send(sockfd, s, len, 0);
}

int main()
{
struct sockaddr_in dest;
// Open socket
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("Create socket error");
exit(errno);
}

// Initialize server address/port struct
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT_INT);
if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 )
{
perror("Cannot understand \""SERVER_ADDR);
exit(errno);
}
#if 0 // if you wish to enable non-blocking mode, enable this
#endif
// Connect to server
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
{
perror("connect");
exit(errno);
}

// Example: login to router and replace route
waitforstring("ogin: ");
sendstring("admin\n");
waitforstring("assword: ");
sendstring("admin\n");
waitforstring(">");
sendstring("sh -c \"route del default;route add default ppp1\"\n");
waitforstring(">");
close(sockfd);
return 0;
}

windows sockets use same BSD sockets maybe just slightly different function prototypes as we used to in Linux or any other *nix systems

docs.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock

Fuck C
#include "asio.hpp"

Problem solved

Still working out Windows's shitty SSL library (schannel) myself user, its hardly documented.
The actual winsock implementation is nearly the same as on Linux/BSD however.
Godspeed.

Ok I tried this but got undefined reference to 'WinMain@16'

By belting out the required calls to winsock.dll. It even has BSD-compatible aliases (connect(), send(), etc) - though you probably want to use the WSA*() versions if you don't care about portability - they're way faster if you use I/O completion ports.

daily homework thread

Bump.

I am not doing your networks homework, just google it and copy pajeet's work

Attached: 1661293545791.gif (498x417, 1.54M)

Read Programming Windows 5th Edition. There's a section on this towards the end.

Or you could read this page: docs.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock

Attached: 512gMw5LwzL.jpg (389x500, 51.21K)