So im programming a client/server setup in C, everything has compiled fine, and my issue here is 2 things :
1. initially, i first have the client tell me to enter a message that is supposed to go to the server, i enter a message and get hit with a "Segmentation Fault : 11"
2. After i get hit with the segmentation fault, the server tells me to enter a message, to send to the client, when i do that i get " Bus error : 10"
In order to summarize
Client error : "Segmentation Fault : 11"
Server error : "Bus error : 10"
client( https://pastebin.com/7Xr7Hcq5)
server( https://pastebin.com/vNLLUSPV)
DO NOT DEAL WITH THIS MEMBER
my nice person u gotta allocate memory for that message
DO NOT DEAL WITH THIS MEMBER
I find it hilarious no one on HF could produce a half decent client/server code
DO NOT DEAL WITH THIS MEMBER
(Jun 10, 2019 08:28 AM)YouthInAsia Wrote: I find it hilarious no one on HF could produce a half decent client/server code
*In this Section only. You do know this section is for people who want to talk about C/C++ Code, Errors and stuff.
DO NOT DEAL WITH THIS MEMBER
(Jun 10, 2019 08:39 AM)lynxmk Wrote: *In this Section only. You do know this section is for people who want to talk about C/C++ Code, Errors and stuff.
Yes but you'd expect people to HAVE AT LEAST A BASIC UNDERSTANDING OF ANY FORM OF NETWORKING / CODE
DO NOT DEAL WITH THIS MEMBER
I fixed your client. It was a simple error. You were taking input in a pointer instead of an char array, I think.
Code #include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int agrc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char message[200];
char server_reply[2000];
if ((sock =socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
//Convert IPV4 and IPV6 from text to binary
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
//keep listening
while(1)
{
printf("Enter Message: ");
scanf("%s" , message);
//Send Data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive Data
if( recv(sock , server_reply , 2000 , 0) < 0 )
{
puts("recv failed");
break;
}
puts("Server Reply");
puts(server_reply);
}
close(sock);
return 0;
}
I looked at the server code but I don't understand what you're trying to create or do. But it looks like you're sending from both sides at the same time and receiving at the same time.
If the server side sends some data, Then the client must receive it. But, Look here; And tell me do you see what I mean?
upload.ee/image/10076060/Screenshot_2019-06-10_20-43-49.png
Jun 10, 2019 01:53 PM
(This post was last modified: Jun 10, 2019 02:14 PM by Truth3356.)
(Jun 10, 2019 09:12 AM)lynxmk Wrote: I fixed your client. It was a simple error. You were taking input in a pointer instead of an char array, I think.
Code #include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int agrc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char message[200];
char server_reply[2000];
if ((sock =socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
//Convert IPV4 and IPV6 from text to binary
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
//keep listening
while(1)
{
printf("Enter Message: ");
scanf("%s" , message);
//Send Data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive Data
if( recv(sock , server_reply , 2000 , 0) < 0 )
{
puts("recv failed");
break;
}
puts("Server Reply");
puts(server_reply);
}
close(sock);
return 0;
}
I looked at the server code but I don't understand what you're trying to create or do. But it looks like you're sending from both sides at the same time and receiving at the same time.
If the server side sends some data, Then the client must receive it. But, Look here; And tell me do you see what I mean?
upload.ee/image/10076060/Screenshot_2019-06-10_20-43-49.png
Yeah i think so, the problem may be i have some code on the server side that should be on the client side, either that or it had to do with the message char.
DO NOT DEAL WITH THIS MEMBER
(Jun 10, 2019 01:53 PM)Truth3356 Wrote: Yeah i think so, the problem may be i have some code on the server side that should be on the client side, either that or it had to do with the message char.
Read my first post, you weren't allocating memory. You could use a pointer, but you'd have to dynamically allocate memory for that pointer. That pointer is just a variable, it literally points to garbage, whatever is at the stack at that time. Hence the segmentation fault.
Jun 10, 2019 04:21 PM
(This post was last modified: Jun 10, 2019 10:02 PM by Truth3356.)
(Jun 10, 2019 04:15 PM)doxerhacker Wrote: Read my first post, you weren't allocating memory. You could use a pointer, but you'd have to dynamically allocate memory for that pointer. That pointer is just a variable, it literally points to garbage, whatever is at the stack at that time. Hence the segmentation fault.
Okay, i will fix that, thanks man.
(Jun 10, 2019 08:28 AM)YouthInAsia Wrote: I find it hilarious no one on HF could produce a half decent client/server code
You gotta give me credit for at least trying.
DO NOT DEAL WITH THIS MEMBER
LOOOL. hahahaha fu*ki*g shitty code. "i don't know how to code so I will ask on Hackforums what is wrong with my copy-pasted code". I mean i came here thinking you were going to ask a specific question related to implementing something from RFC docs or something else along this line which would be no problem)), instead you gave me a laugh hahahaha
(Jun 13, 2019 08:07 AM)MaximumPain Wrote: LOOOL. hahahaha fu*ki*g shitty code. "i don't know how to code so I will ask on Hackforums what is wrong with my copy-pasted code". I mean i came here thinking you were going to ask a specific question related to implementing something from RFC docs or something else along this line which would be no problem)), instead you gave me a laugh hahahaha
Instead of motherfuckin helping, you shitposted and made fun of OP? I dare you post some code you written on your first day of coding. duh.
Jun 13, 2019 11:53 AM
(This post was last modified: Jun 13, 2019 11:57 AM by Truth3356.)
(Jun 13, 2019 08:46 AM)dracx Wrote: Instead of motherfuckin helping, you shitposted and made fun of OP? I dare you post some code you written on your first day of coding. duh.
Dont worry, i fixed it, it works now.
(Jun 13, 2019 08:07 AM)MaximumPain Wrote: LOOOL. hahahaha fu*ki*g shitty code. "i don't know how to code so I will ask on Hackforums what is wrong with my copy-pasted code". I mean i came here thinking you were going to ask a specific question related to implementing something from RFC docs or something else along this line which would be no problem)), instead you gave me a laugh hahahaha
Theres zero wrong with asking for help. I didn't ask anyone to just do the whole thing for me. I never asked to have my hand held through it. I had a question, and i self-advocated and asked for help.
I don't know if you have fixed your problem. However, a quick glance through the code revealed one telling problem. In the client, you declare char*message but never allocate a buffer (new char[size_of_buffer]). If memory serves correctly, scanf requires a buffer not a pointer to null (which is what you pass in your case).
Hope this 'points' you in the right direction.
|