https://github.com/seleznevae/libasock
Asynchronous sockets for Linux
https://github.com/seleznevae/libasock
c linux networking sockets
Last synced: about 2 months ago
JSON representation
Asynchronous sockets for Linux
- Host: GitHub
- URL: https://github.com/seleznevae/libasock
- Owner: seleznevae
- License: mit
- Created: 2017-11-21T17:33:40.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-21T20:03:51.000Z (over 8 years ago)
- Last Synced: 2025-01-24T10:44:54.746Z (over 1 year ago)
- Topics: c, linux, networking, sockets
- Language: C
- Homepage:
- Size: 122 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libasock (WIP)
[](https://travis-ci.org/seleznevae/libasock)
[](https://opensource.org/licenses/MIT)
Simple library for asynchronous sockets in linux.
# Examples
```
/* Client example */
#include "asock.h"
#include
#include
#include
#include
#include
int main()
{
asock_init();
asock_t *client = asock_create(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(8888);
asock_connect(client, (struct sockaddr*)&sa, sizeof(struct sockaddr));
while (asock_state(client) == InProgress)
sleep(1);
if (asock_state(client) == Established) {
const char *message = "Hello, server!\n";
asock_write(client, message, strlen(message));
char c;
while (1) {
if (asock_read(client, &c, sizeof(c)) > 0)
fprintf(stderr, "%c", c);
}
}
asock_destroy(client);
asock_deinit();
return 0;
}
```
```
/* Server example */
/* Server example */
#include "asock.h"
#include
#include
#include
#include
#include
int main()
{
asock_init();
asock_server_t *server = asock_server_create(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(8888);
asock_server_bind(server, (struct sockaddr*)&sa, sizeof(struct sockaddr));
asock_server_listen(server);
asock_t *connection = NULL;
while (1) {
connection = asock_server_accept(server);
if (connection == NULL)
sleep(1);
else
break;
}
while (asock_state(connection) == InProgress)
sleep(1);
if (asock_state(connection) == Established) {
const char *message = "Hello, client!\n";
asock_write(connection, message, strlen(message));
sleep(1);
while (1) {
char c;
if (asock_read(connection, &c, sizeof(c) > 0))
fprintf(stderr, "%c", c);
}
}
asock_destroy(connection);
asock_server_destroy(server);
asock_deinit();
return 0;
}
```