|
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 |
|