Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************
- * Ett enkelt exempel för att testa sockets i windowsmiljö *
- * med WSA (klientdel) *
- * *
- * Kurs: Nätverksprogrammering, DVA209 *
- * Kod av: Robert Suurna (lärare, mdh) *
- * Datum: 2011-10-07 *
- * *
- * struct sockaddr_in{ *
- * short sin_family; *
- * unsigned short sin_port; *
- * struct in_addr sin_addr; *
- * char sin_zero[8]; *
- * }; *
- ***********************************************************/
- #include <windows.h>
- #include <winsock2.h>
- #include <stdio.h>
- #include <string.h>
- #define NETWORK_ERROR -1
- #define NETWORK_OK 0
- #define CONN_PORT 8888
- void sendmsg(SOCKET clientSocket);
- void report_error(const char *);
- int main(void){
- WORD sockVersion;
- WSADATA wsaData;
- SOCKET clientSocket;
- struct sockaddr_in serverInfo;
- int sError;
- char ip_address[20];
- sockVersion = MAKEWORD(2, 0);
- printf("IP-Address:");
- gets(ip_address);
- // Fill a socketaddr_in struct with address information
- serverInfo.sin_family = AF_INET;
- serverInfo.sin_addr.s_addr = inet_addr(ip_address);
- serverInfo.sin_port = htons(CONN_PORT); // Change to network-byte order and insert into port field
- // Initialize Winsock
- WSAStartup(sockVersion, &wsaData);
- // Create the socket
- clientSocket = socket(AF_INET, // Go over TCP/IP
- SOCK_STREAM, // A stream-oriented socket
- IPPROTO_TCP); // Use TCP protocol
- if(clientSocket == INVALID_SOCKET) {
- report_error("socket()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- printf("\nCreate socket ok!\n");
- printf("\nTrying to connect to server (%s)...", ip_address);
- // Connect to the server
- sError = connect(clientSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
- if(sError == SOCKET_ERROR) {
- report_error("connect()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- // Successfully connected
- printf("\nConnected to server (%s)!\n", ip_address);
- // Send to server (and receive from server)
- // ---> Put your new function call here! <---
- sendmsg(clientSocket);
- // system("PAUSE");
- printf("\nDisconnected from server!\n\n");
- // Clean up!
- closesocket(clientSocket);
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- /********************************************************************
- * Function grabs the last socket error number and displays an error *
- ********************************************************************/
- void report_error(const char *whichFunc)
- {
- int errorCode;
- char errorMsg[100]={};
- errorCode = WSAGetLastError(); // Grab last error message
- // Generate error string
- sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
- // Print error message in console window
- printf("\n%s",errorMsg);
- // Popup window with error message, just for fun
- MessageBox(NULL, errorMsg, "socket indication", MB_OK);
- }
- void sendmsg(SOCKET clientSocket) {
- char msg[100];
- int length;
- while(1) {
- printf("Please type in a string\n");
- printf("Type %c#%c to disconnect.\n", 34, 34);
- fflush(stdin);
- gets(msg);
- length = strlen(msg);
- if(msg[0] == '#')
- break;
- send(clientSocket, msg, length, 0);
- }
- }
- Serv
- /***********************************************************
- * Ett enkelt exempel för att testa sockets i windowsmiljö *
- * med WSA (serverdel) *
- * *
- * Kurs: Nätverksprogrammering, DVA209 *
- * Kod av: Robert Suurna (lärare, mdh) *
- * Datum: 2011-10-07 *
- * *
- * struct sockaddr_in{ *
- * short sin_family; *
- * unsigned short sin_port; *
- * struct in_addr sin_addr; *
- * char sin_zero[8]; *
- * }; *
- ***********************************************************/
- #include <windows.h>
- #include <winsock2.h>
- #include <stdio.h>
- #define NETWORK_ERROR -1
- #define NETWORK_OK 1
- #define CONN_PORT 8888
- int recvmsg(SOCKET clientSocket);
- void report_error(const char *);
- int main(void) {
- WORD sockVersion;
- WSADATA wsaData;
- SOCKET listeningSocket, clientSocket;
- struct sockaddr_in serverInfo, clientInfo;
- int sError, error, size, listening=1;
- sockVersion = MAKEWORD(2, 0); // Use Winsock version 2.0
- // Use sockaddr_in struct to fill in address information
- serverInfo.sin_family = AF_INET;
- serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listening for connections, any local address will do
- serverInfo.sin_port = htons(CONN_PORT); // Convert integer to network-byte order and insert into the port field
- // Initializing Winsock
- error = WSAStartup(sockVersion, &wsaData);
- // Check for error
- if (error != 0) {
- report_error("WSAStartup()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- // Create the listening socket
- listeningSocket = socket(AF_INET, // Use TCP/IP
- SOCK_STREAM, // A stream-oriented socket
- IPPROTO_TCP); // Use TCP protocol
- if (listeningSocket == INVALID_SOCKET) {
- report_error("socket()"); // Report the error with our custom function
- WSACleanup(); // Shutdown Winsock
- return NETWORK_ERROR;
- }
- printf("\nCreate socket ok!");
- // Bind the socket to our local server address
- sError = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr_in));
- if (sError == SOCKET_ERROR) {
- report_error("bind()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- printf("\nBind ok!");
- // Make the socket listen. Up to 10 connections may wait at any one time to be accepted
- sError = listen(listeningSocket, 10);
- if (sError == SOCKET_ERROR) {
- report_error("listen()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- printf("\nSocket is now in listen mode!\n");
- while(listening) {
- printf("\nWaiting for a client...");
- // Wait for a client to connect, if accepted store info from connecting client in clientInfo
- size = sizeof(clientInfo);
- clientSocket = accept(listeningSocket, (struct sockaddr *)&clientInfo, &size);
- if (clientSocket == INVALID_SOCKET) {
- report_error("accept()");
- WSACleanup();
- return NETWORK_ERROR;
- }
- printf("\nConnect from client %s, port %d\n",inet_ntoa(clientInfo.sin_addr),
- ntohs(clientInfo.sin_port));
- // Receive from client (and send to client)
- // ---> Put your new function call here! <---
- while(recvmsg(clientSocket) > 0);
- closesocket(clientSocket);
- printf("\nClient disconnected!\n\n");
- }
- closesocket(listeningSocket);
- printf("\nListening socket closed!\n\n");
- // Shutdown Winsock
- WSACleanup();
- return NETWORK_OK;
- }
- /********************************************************************
- * Function grabs the last socket error number and displays an error *
- ********************************************************************/
- void report_error(const char *whichFunc)
- {
- int errorCode;
- char errorMsg[100]={};
- errorCode = WSAGetLastError(); // Grab last error message
- // Generate error string
- sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
- // Print error message in console window
- printf("\n%s",errorMsg);
- // Popup window with error message, just for fun
- MessageBox(NULL, errorMsg, "socket indication", MB_OK);
- }
- int recvmsg(SOCKET clientSocket) {
- char msg[100];
- int recieve;
- recieve = recv(clientSocket, msg, 20, 0);
- if(recieve > 0) {
- if(msg[0] == '#')
- return 0;
- printf("%s\n", msg);
- }
- return recieve;
- }
Advertisement
Add Comment
Please, Sign In to add comment