| |

VerySource

 Forgot password?
 Register
Search
View: 1685|Reply: 10

No work, no work

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-21 13:00:01
| Show all posts |Read mode
The company did IM, so I went in and asked me to write some small programs. The server listens and displays all characters sent by the client. The client may have 100,000 characters. Run under win.
The program is written in C ++ BUILDER. "Socket error" is reported when running.
code show as below.
I want to ask 2 questions:
1. Why does the socket fail?
2. In the for loop, I want to use multi-threaded processing, how to achieve it?

 #include <winsock2.h>
 #include <stdio.h>

 #define PORT 1234
 #define SERVER_IP "127.0.0.1"

int main (int argc, char * argv [])
{
    int sockfd, new_fd;
    struct sockaddr_in addr;
    

    if (INVALID_SOCKET == (sockfd = socket (AF_INET, SOCK_STREAM, 0)))
    {
       printf ("socket error\n");
       goto end;
    }

    memset (&addr, 0, sizeof (addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons (PORT);
    addr.sin_addr.s_addr = inet_addr (SERVER_IP);


    if (connect (sockfd, (const struct sockaddr FAR *)&addr, sizeof (addr)))
    {
       printf ("connect error\n");
       goto end;
    }

    if (listen (sockfd, SOMAXCONN))
    {
       printf ("listen error\n");
       goto end;
    }

    for (;;)
    {
       // how to deal with
    }

    end:
    system ("pause");
    return 0;
}
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-1-30 23:00:01
| Show all posts
Windows platform needs to be initialized with WSAStartup
     #define sockstart (name)\
                do {\
                        WSADATA name;\
                        if (WSAStartup (MAKEWORD (1,1),&name))\
                                perror ("sockstart");\
                } while (0)

        #define sockend () WSACleanup ()

sockstart (wsad);

100,000 clients on one machine?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-31 12:36:01
| Show all posts
Add the above code, the socket can be normal, but the connection fails again

The above code is server-side
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 17:47:58
| Show all posts
Is socket used naked? Have you found a ready-made library or package
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-31 18:09:01
| Show all posts
There are many good design patterns for multithreading, such as thread pools, etc.
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-2-1 05:18:01
| Show all posts
I don't know anything, what should I do?
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-2 12:18:01
| Show all posts
#include <winsock2.h>
 #include <stdio.h>

 #define PORT 1234
 #define SERVER_IP "127.0.0.1"

int main (int argc, char * argv [])
{
    int sockfd, new_fd;
    struct sockaddr_in addr;
    

    if (INVALID_SOCKET == (sockfd = socket (AF_INET, SOCK_STREAM, 0))) // This condition?
    {
       printf ("socket error\n");
       goto end;
    }

    memset (&addr, 0, sizeof (addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons (PORT);
    addr.sin_addr.s_addr = inet_addr (SERVER_IP);


    if (connect (sockfd, (const struct sockaddr FAR *)&addr, sizeof (addr)))
// The server first bind connect?
    {
       printf ("connect error\n");
       goto end;
    }

    if (listen (sockfd, SOMAXCONN))
    {
       printf ("listen error\n");
       goto end;
    }
// Create a thread for receiving and sending
    for (;;)
    {
       // how to deal with
    }

    end:
    system ("pause");
    return 0;
}
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-14 11:30:02
| Show all posts
Modify it and replace connect with bind, just fine.

The code now looks like this:

 void mythread (void * arg)
 {
      char buffer [MAX_LENGTH];
      int sockfd = * (SOCKET *) arg;
      recv (sockfd, buffer, MAX_LENGTH, 0);
      printf ("% s\n", buffer);
      _endthread ();
 }

main ()
{
   // do something

   for (;;)
    {
        new_fd = accept (sockfd, 0,0);
        if (INVALID_SOCKET! = new_fd)
        {
            _beginthread (mythread, 0,&new_fd);
        }
        else
        {
            printf ("wait\n");
        }
    } // end for
  
    closesocket (sockfd);
}

my question is
1. Do you want to close socksfd before beginthread
2. Right way to create a new thread
3. If it is not connected, the accept is called continuously, continuously outputting "wait", which consumes resources,
   How to block accept and wait until there is a connection.
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-14 17:00:01
| Show all posts
Learn. But what I write is blocking.

#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>

void main ()
{
struct sockaddr_in test;
struct sockaddr_in re;
FILE * p = NULL;
SOCKET test1, test2;
int a, b;
WORD wVersion = MAKEWORD (2,0);
WSADATA wsData;

char buff [60];
int nResult = WSAStartup (wVersion,&wsData);

test.sin_port = 4000; // guaranteed byte order
test. sin_addr.s_addr = htonl (INADDR_ANY);
test.sin_family = AF_INET;
b = sizeof (struct sockaddr_in);
test1 = socket (AF_INET, SOCK_STREAM, 0);

if (test1 == INVALID_SOCKET)
{
exit (0);
}

a = bind (test1, (struct sockaddr *)&test, sizeof (struct sockaddr));
if (0! = a)
{
printf ("bind err\n");
exit (0);
}


a = listen (test1,1);

test2 = accept (test1, (struct sockaddr *)&re,&b);
if (test2! = INVALID_SOCKET)
{
printf ("connt\n");
}

p = fopen ("test.exe", "wb +");

while (1)
{
a = recv (test2,&buff [0], 50,0);
Ranch
if (0 == a)
{
a = WSAGetLastError ();
printf ("recv err\n");
exit (0);
} else
{
fwrite (buff, 50,1, p);
printf ("% s\n", buff);
}
}

a = send (test2,&buff [0], sizeof (buff), 0);

test2 = accept (test1, (struct sockaddr *)&re,&b);

}
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-3-1 21:45:01
| Show all posts
Under Windows, you can try to complete the port, thread pool, etc.
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