Permalink
Cannot retrieve contributors at this time
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?
CSE4402-TCP-Attacks-Lab/tcp_client.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (28 sloc)
1002 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |