--Answered now through comment, thanks to all who helped out--
Apologies in advance for my lack of knowledge, C isn't my strong suit. I've been working with the Windows Socket API quite a bit recently in my windows networking class. I've set up a server network where I can (in theory) connect to my other devices using winsock, provided they give authorization through the use of another program. Once authorized, I have this server program that I can use to connect and work with my machine. However, I've run into a problem that I simply cannot seem to solve. Every time I run this program, it seems to connect properly (displaying the Shell line, through this has a warning attached to it), but hits the _write line and fails. Here is a list of things I've considered/tried using to fix it:
- The int(main) function exceeds the stack size, though I'm unsure if this would effect the _write function.
- The printf("* Shell#%~$: ", inet_ntoa(client_address.sin_addr)); line is supposed to look like a linux command line and display the address of my machine. However, the compiler generates C6271 the function doesn't work properly.
- I've been told that this could be due to sizeof(buffer) always returning 1 because it is a char with size 1024, but changing this didn't seem to have any effect.
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <ws2tcpip.h>
#include <winerror.h>
#pragma comment(lib, "ws2_32.lib")
#define bzero(p, size) (void) memset((p), 0, (size))
int main()
{
WSADATA wsaData;
WSAStartup(2.2, &wsaData);
int sock, client_socket;
char buffer [1024];
char response[18384];
struct sockaddr_in server_address, client_address;
int i = 0;
int optval = 1;
socklen_t client_length;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
{
wchar_t* s = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&s, 0, NULL);
fprintf(stderr, "%S
", s);
LocalFree(s);
printf("Error Setting TCP Socket Options!
");
return 1;
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("70.69.238.0");
server_address.sin_port = htons(50005);
bind(sock, (struct sockaddr*)&server_address, sizeof(server_address));
listen(sock, 5);
client_length = sizeof(client_address);
client_socket = accept(sock, (struct sockaddr*)&client_address, &client_length);
while (1)
{
jump:
bzero(&buffer, sizeof(buffer));
bzero(&response, sizeof(response));
printf("* Shell#%s~$: ", inet_ntoa(client_address.sin_addr));
fgets(buffer, sizeof(buffer), stdin);
strtok(buffer, "
");
_write(client_socket, buffer, sizeof(buffer));
if (strcmp("q", buffer, 1) == 0)
{
closesocket(sock);
WSACleanup();
exit(0);
break;
}
else
{
recv(client_socket, response, sizeof(response), MSG_WAITALL);
printf("%s", response);
}
}
}
Debug Output
File: minkernelcrtsucrtsrcappcrtlowiowrite.cpp Line: 49
Expression: (fh >= 0 && (unsigned)fh < (unsigned)_nhandle)
For information on how your program can cause an assertion failure,
see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
Any help would be greatly appreciated, and if anything else is needed to make it more clear, I can always submit it.