| |

VerySource

 Forgot password?
 Register
Search
View: 651|Reply: 4

Socket applet, variables inexplicably changed

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-23 18:20:01
| Show all posts |Read mode
Client:
#include <stdio.h>
#include <stdlib.h>
#include <sys / types.h>
#include <sys / socket.h>
#include <netinet / in.h>
#include <pthread.h>
#include <errno.h>

void * client (void * p)
{
int sockfd, new_fd, i = 0, j = 0, nbytes;
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
char * tmp = (char *) p;

char ip [20], port [6];

while (tmp [i]! = ':')
{
ip [i] = tmp [i];
i ++;
}
ip [i ++] = '\0';

while (tmp [i]) port [j ++] = tmp [i ++];
port [j] = '\0';


for (i = 0; i <10000; ++ i)
{
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd ==-1)
{
printf ("Client Error.% s\n", strerror (errno));
return;
}

s_addr.sin_family = AF_INET;
s_addr.sin_port = htons (atoi (port)); // The second time the port changes
s_addr.sin_addr.s_addr = inet_addr (ip);
bzero (&(s_addr.sin_zero), sizeof (s_addr.sin_zero));


while (connect (sockfd, (struct sockaddr *)&s_addr, sizeof (struct sockaddr)) ==-1)
{
printf ("Client Error.% s\n", strerror (errno));
}

char buffer [10];

if ((nbytes = read (sockfd, buffer, 1024)) ==-1)
{
printf ("Client Error.% s\n", strerror (errno));
}

if (buffer [0] == 'M')
printf ("% s\n", buffer);

close (sockfd);
}
return;
}


int main (int argc, char * argv [])
{

if (argc <2)
{
printf ("Usage: 1.out server's IP: server's port");
printf ("Example: 1.out 192.168.0.1:4444\n");
return -1;
}

client (argv [1]);

return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Service-Terminal:
#include <stdio.h>
#include <stdlib.h>
#include <sys / types.h>
#include <sys / socket.h>
#include <netinet / in.h>
#include <pthread.h>
#include <errno.h>

void * server (void * p)
{
int sockfd, new_fd, i = 0, j = 0;
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
char * tmp = (char *) p;

char ip [20], port [6];

while (tmp [i]! = ':')
{
ip [i] = tmp [i];
i ++;
}
ip [i ++] = '\0';

while (tmp [i]) port [j ++] = tmp [i ++];
port [j] = '\0';

sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd ==-1)
{
printf ("Server Error.% s\n", strerror (errno));
return;
}

s_addr.sin_family = AF_INET;
s_addr.sin_port = htons (atoi (port));
s_addr.sin_addr.s_addr = inet_addr (ip);
bzero (&(s_addr.sin_zero), sizeof (s_addr.sin_zero));

if (bind (sockfd, (struct sockaddr *)&s_addr, sizeof (struct sockaddr)) ==-1)
{
printf ("Server Error.% s\n", strerror (errno));
return;
}

if (listen (sockfd, 500) ==-1)
{
printf ("Server Error.% s\n", strerror (errno));
return;
}

char s [32], c [6];

for (i = 0; i <10000; ++ i)
{
s [0] = '\0';
if (i% 3 == 0) strcpy (s, "M");
sprintf (c, "% d", i);
strcat (s, c);
strcat (s, "\n");

if ((new_fd = accept (sockfd, NULL, NULL)) ==-1)
printf ("Server Error.% s\n", strerror (errno));
printf ("% s\n", s);
write (new_fd, s, sizeof (s));
close (new_fd);
}

close (sockfd);
return;
}



int main (int argc, char * argv [])
{

if (argc <2)
{
printf ("Usage: 1.out server's IP: server's port client's");
printf ("Example: 1.out 192.168.0.1:4444\n");
return -1;
}

server (argv [1]);

return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Using gdb to trace the client program, I found that the port itself has changed, causing the connection to fail.
gcc 3.2.2
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-7 16:45:02
| Show all posts
?
Can the port be fixed?
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-8 00:30:01
| Show all posts
Put the cleared program outside the loop, and that's it ...
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-8 13:15:02
| Show all posts
The error is

char buffer [10];

if ((nbytes = read (sockfd, buffer, 1024)) ==-1)
{
printf ("Client Error.% s\n", strerror (errno));
}

Overflowed
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-8 13:30:01
| Show all posts
char buffer [10];

if ((nbytes = read (sockfd, buffer, 1024)) ==-1)
Could it be a buffer overflow modification ...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list