Skip to content

Instantly share code, notes, and snippets.

@habakan
Created June 26, 2017 03:11
Show Gist options
  • Save habakan/a2b62b09185b5cce960cb00c24261199 to your computer and use it in GitHub Desktop.
Save habakan/a2b62b09185b5cce960cb00c24261199 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
struct sockaddr_in server;
int sock;
char buf[32];
int n;
sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_port = htons(12345);
/*通信先(サーバーのアドレスを指定)*/
//server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_addr.s_addr = inet_addr("192.168.33.10");
/* サーバに接続 */
connect(sock, (struct sockaddr *)&server, sizeof(server));
/* サーバからデータを受信 */
memset(buf, 0, sizeof(buf));
n = read(sock, buf, sizeof(buf));
printf("%d, %s\n", n, buf);
/* socketの終了 */
close(sock);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment