Question
I have these methods for a client-server chat app whenever I send a message on the server side it works but whenever I try doing
I have these methods for a client-server chat app
whenever I send a message on the server side it works but whenever I try doing it on the client it gives me an error
client:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
#include "Client.h"
SOCKET s;
int Client::init(uint16_t port, char* address)
{
int result = 0;
sockaddr_in servaddr;
bzero((char*)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.S_un.S_addr = inet_addr(address);
servaddr.sin_port = htons(port);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
return SETUP_ERROR;
}
result = inet_pton(AF_INET, address, &(servaddr.sin_addr));
if (result == 0 || result == -1)
{
return ADDRESS_ERROR;
}
result = connect(s, (sockaddr*)&servaddr, sizeof(servaddr));
if (result == SOCKET_ERROR)
{
if (result == 0)
{
return CONNECT_ERROR;
}
else
{
return SHUTDOWN;
}
}
else
{
return SUCCESS;
}
}
int Client::readMessage(char* buffer, int32_t size)
{
int sizeOrError = recv(s, buffer, size, 0);
if (sizeOrError > size)
{
return PARAMETER_ERROR;
}
if (sizeOrError == SOCKET_ERROR)
{
if (sizeOrError == 0)
{
return SHUTDOWN;
}
else
{
return DISCONNECT;
}
}
else
{
return SUCCESS;
}
}
int Client::sendMessage(char* data, int32_t length)
{
if (length < 0 || length > 255)
{
return PARAMETER_ERROR;
}
int sizeOrError = send(s, data, length, 0);
if (sizeOrError == SOCKET_ERROR)
{
if (sizeOrError == 0)
{
return DISCONNECT;
}
else
{
return SHUTDOWN;
}
}
else
{
return SUCCESS;
}
}
void Client::stop()
{
shutdown(s, SD_BOTH);
close(s);
}
server:
#include "Server.h"
#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
SOCKET s;
SOCKET s2;
int Server::init(uint16_t port)
{
int result = 0;
sockaddr_in servaddr;
bzero((char*) &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
return SETUP_ERROR;
}
result = bind(s, (sockaddr*)&servaddr, sizeof(servaddr));
if (result == INVALID_SOCKET)
{
return BIND_ERROR;
}
result = listen(s, 1);
if (result == SOCKET_ERROR)
{
return SETUP_ERROR;
}
s2 = accept(s, NULL, NULL);
if (s2 == INVALID_SOCKET)
{
if (s2 == SOCKET_ERROR)
{
return CONNECT_ERROR;
}
else
{
return SHUTDOWN;
}
}
else
{
return SUCCESS;
}
}
int Server::readMessage(char* buffer, int32_t size)
{
s2 = recv(s2, buffer, size, 0);
if (s2 > size)
{
return PARAMETER_ERROR;
}
if (s2 == SOCKET_ERROR)
{
if (s2 == 0)
{
return DISCONNECT;
}
else
{
return SHUTDOWN;
}
}
else
{
return SUCCESS;
}
}
int Server::sendMessage(char* data, int32_t length)
{
if (length < 0 || length > 255)
{
return PARAMETER_ERROR;
}
s2 = send(s2, data, length, 0);
if (s2 == SOCKET_ERROR)
{
if (s2 == 0)
{
return DISCONNECT;
}
else
{
return SHUTDOWN;
}
}
else
{
return SUCCESS;
}
}
void Server::stop()
{
shutdown(s, SD_BOTH);
close(s);
close(s2);
}
Edit View Git Project Build Debug Test HA Process: [40664] Spaghetti Relay GUI.exe File Server.ha ++ Server 63 64 97% 65 66 67 68 69 70 71 73 74 75 76 77 78 79 80 81 88 89 90 91 92 93 94 95 96 97 B Ready 8 18 Watch 1 Search (Ctrl+E) Name Server.cp | } Hint Serv } else { } if { else { 1 72F Mostly cloudy Spaghetti Relay Autos Locals Watch 1 File Server Mode void Server::stop() { Address 127.0.0.1 return SUCCESS; No issues found Debug [Spaghetti Relay Program] [Connected] [Sent Out]: hello O Client Mode shutdown(s, how: SD_BOTH); close(s); close(s2); 0 Continue ey - x86 Lifecycle Events Thread: [47744] clr.dll thread Port 31337 Analyze Search Depth: Value 0 Connected O Send Tools Extensions Stop X PP Serve H Window G File Help II PA Spaghetti Relay = Address O Server Mode Search (Ctrl+Q) 127.0.0.1 Client Mode [Spaghetti Relay Program] [Connected] [Received]: hello ? Stack Frame: Server::readMessage Port 31337 0 Connected Type a Send Stop 0 x Spaghetti Relay GUI 2 x Output Application Insights sendMessage(char* data, int32_t length) SpaghettiForm.cs X Diagnostic Tools 10 Ln: 70 Ch: 2 TABS CRLF Exception Settings Call Stack Breakpoints Command Window Immediate Window Output Error List Diagnostics session: 30 seconds 20s Events 4 Process Memory (MB) 19 Sign in 0 CPU (% of all processors) 100 0 Show Events (0 of 0) Memory Usage Summary Events Memory Usage CPU Usage Events CPU Usage O Record CPU Profile Live Share Take Snapshot Enable heap profiling (affects performance) Add to Source Control s P.. ENG US 4 x Show output from: Debug 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Users\sirin\OneDrive\Documentos\Work Folder\CNE\week 1\SpaghettiRelay\SpaghettiRelay 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 'Spaghetti Relay GUI.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\ucrtbased.dll' 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mswsock.dll'. 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Users\sirin\OneDrive\Documentos\Work Folder\CNE\week 1\SpaghettiRelay\SpaghettiRelay 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 'Spaghetti Relay GUI.exe' (Win32): Loaded "C:\Windows\SysWOW64\vcruntime140d.dll'. 'Spaghetti Relay GUI.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mswsock.dll'. Select Repository 0 100 0 11:39 PM 11/10/2022 Solution Explorer Git Changes La
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started