Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
int main(){
//Step 1: Create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Step 2: Set the destination info
struct sockaddr_in dest;
memset(&dest, 0, sizeof(struct sockaddr_in));
dest.sin_family = AF_INET;
//You are going to have to change the IP address in the line below to the IP address of the machine you are using as the server. In my case, 10.0.2.8
dest.sin_addr.s_addr = inet_addr("10.0.2.8");
dest.sin_port = htons(9090);
//Step 3: Connect to the server
connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr_in));
//Step 4: send data to server
//The data we are sending is two static strings
char* buffer1 = "Hello Server!\n";
char* buffer2 = "Hello Again!\n";
write(sockfd, buffer1, strlen(buffer1));
write(sockfd, buffer2, strlen(buffer2));
//Step 5: close the connection
close(sockfd);
return 0;
}//ENDMAIN