Java does not suppport DGRAM sockets
This commit is contained in:
parent
965e3d18c8
commit
14ba9ab891
1 changed files with 11 additions and 19 deletions
|
@ -11,29 +11,21 @@ int main(void) {
|
|||
server_addr.sun_family = AF_UNIX;
|
||||
strcpy(server_addr.sun_path, SERVER_SOCK_FILE); // XXX: should be limited to about 104 characters, system dependent
|
||||
|
||||
struct sockaddr_un client_addr = {};
|
||||
client_addr.sun_family = AF_UNIX;
|
||||
strcpy(client_addr.sun_path, CLIENT_SOCK_FILE);
|
||||
|
||||
// get socket
|
||||
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
// bind client to client_filename
|
||||
if (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr)) > 0) {
|
||||
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) > 0) {
|
||||
unsigned char* messageBuffer = malloc(500);
|
||||
memset(messageBuffer, 0, 500);
|
||||
const char* message = "Hello from C";
|
||||
size_t messageLengthInBytes = strlen(message);
|
||||
messageBuffer[0] = messageLengthInBytes;
|
||||
strcpy((char *)messageBuffer+1, message);
|
||||
send(sockfd, messageBuffer, messageLengthInBytes+1, 0);
|
||||
free(messageBuffer);
|
||||
}
|
||||
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) > -1) {
|
||||
unsigned char messageBuffer[500] = {};
|
||||
const char* message = "Hello from C";
|
||||
size_t messageLengthInBytes = strlen(message);
|
||||
messageBuffer[0] = messageLengthInBytes;
|
||||
// Lucky for us the message just happens to be compatible with UTF-8 encoding
|
||||
strcpy((char *)messageBuffer+1, message);
|
||||
send(sockfd, messageBuffer, messageLengthInBytes+1, 0);
|
||||
} else {
|
||||
perror("Unable to connect") ;
|
||||
}
|
||||
|
||||
// connect client to server_filename
|
||||
|
||||
|
||||
unlink (CLIENT_SOCK_FILE);
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue