| |

VerySource

 Forgot password?
 Register
Search
View: 1761|Reply: 11

How to write data to a socket

[Copy link]

1

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 11:40:01
| Show all posts |Read mode
I wrote a udp server program. In some cases, the server needs to write data into the socket itself.
I tried, write (sockfd, data, 10) directly; it doesn't work, the system prompts Destination address required
Unless the server simulates the client to connect to itself, and then can write data, is there a simpler way?
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-4 11:42:01
| Show all posts
Can be written, that is, write / send, it is estimated that there is a problem with your code
Reply

Use magic Report

1

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-4 11:57:01
| Show all posts
The situation you said is the method often used by the udp client.
Bind the socket and struct sockaddr_in with connect () beforehand,
This can be done without using sendto, write () or send () directly without problems,
But mine is server-side, not work
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 12:03:01
| Show all posts
socket is an interface after encapsulating the protocol
He provides just a few function interfaces for you to use
According to a series of descriptions he provided
Determine if the return is correct
Confirm that the connection is set up correctly
Reply

Use magic Report

1

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-4 12:12:01
| Show all posts
In other words, can the server internally write data to the socket without going through the network (too much trouble)?
Reply

Use magic Report

1

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-4 12:24:01
| Show all posts
In other words, can the server internally write data to its socket without going through the network (too much trouble)?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 13:30:01
| Show all posts
server:
    recv (pkt)
    packet_dispatch (pkt)

So you can call packet_dispatch?
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-1-4 13:33:01
| Show all posts
sendto, can you fill in the address yourself?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 14:24:01
| Show all posts
The first is whether the server can answer, and then the data you write is consistent with what the server requires. Using sendto or send can definitely send the data out, you try again.
Reply

Use magic Report

1

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-4 15:54:01
| Show all posts
Thank you several brothers upstairs! !!
tobetter:
Do not understand what you mean? Can you say more in detail.
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