Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* **************************************************
- Simon Persson
- 9011290039
- spn13004
- Labb 5 uppgift 2 KLIENT
- **************************************************
- */
- /***********************************************************
- * 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>
- #define NETWORK_ERROR -1
- #define NETWORK_OK 0
- #define IP_ADDRESS "127.0.0.1"
- #define CONN_PORT 8888
- #include <conio.h> /* getch() and kbhit() */
- void taemot(clientSocket);
- void report_error(const char *);
- void skicka(clientSocket);
- int main(void){
- WORD sockVersion;
- WSADATA wsaData;
- SOCKET clientSocket;
- struct sockaddr_in serverInfo;
- int sError;
- char arr[512], arr2[512];
- //char IP_ADDRESS[15];
- sockVersion = MAKEWORD(2, 0);
- //printf("Mata in ip-adressen till servern: ");
- //gets(IP_ADDRESS); // Tar emot ip adress
- // Fill a socketaddr_in struct with address information
- serverInfo.sin_family = AF_INET;
- serverInfo.sin_addr.s_addr = inet_addr(IP_ADDRESS); //lägger in ip-adressen i structen
- 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!");
- 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);
- int i;
- unsigned long on = 1;
- ioctlsocket(clientSocket, FIONBIO, &on);
- _beginthread(taemot, 0, clientSocket);
- _beginthread(skicka, 0, clientSocket);
- while (1){
- while (!kbhit()) {
- taemot(clientSocket);
- }
- skicka(clientSocket);
- /*
- //taemot(clientSocket);
- //
- while (!kbhit()) {
- if(recv(clientSocket, arr2,sizeof(arr2),0) > 0)
- printf("\n%s \n", arr2);
- }
- if(kbhit()) {
- gets(arr);
- if(send(clientSocket, arr,sizeof(arr),0)>0)
- fflush(stdin);
- }
- */
- }
- 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 taemot(clientSocket){
- char arr2[255];
- memset(arr2,0,sizeof(arr2));
- if(recv(clientSocket, arr2,sizeof(arr2),0) > 0)
- printf("\n%s \n", arr2);
- }
- void skicka(int clientSocket){
- char arr[255];
- if(kbhit())
- gets(arr);
- if(send(clientSocket, arr,sizeof(arr),0) > 0)
- fflush(stdin);
- memset(arr,0,sizeof(arr));
- }
- 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);
- }
- ************************************************
- /* **************************************************
- Simon Persson
- 9011290039
- spn13004
- Labb 5 uppgift 2 SERVER
- **************************************************
- */
- /***********************************************************
- * 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
- #include <process.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define STDIN 0
- #include <conio.h> /* getch() and kbhit() */
- #include <unistd.h>
- void taemot(clientSocket);
- void report_error(const char *);
- void skicka(clientSocket);
- int main(void) {
- WORD sockVersion;
- WSADATA wsaData;
- SOCKET listeningSocket, clientSocket;
- struct sockaddr_in serverInfo, clientInfo;
- int sError, error, size, listening=1;
- char arr[512], arr2[512];
- 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));
- // where socketfd is the socket you want to make non-blocking
- unsigned long on = 1;
- ioctlsocket(clientSocket, FIONBIO, &on);
- _beginthread(skicka, 0, clientSocket);
- _beginthread(taemot, 0, clientSocket);
- while (1){
- while (!kbhit()) {
- taemot(clientSocket);
- }
- skicka(clientSocket);
- //if(recv(clientSocket, arr2,sizeof(arr2),0) > 0)
- // printf("\n%s \n", arr2);
- //}
- //if(kbhit())
- // gets(arr);
- //if(send(clientSocket, arr,sizeof(arr),0) > 0)
- //fflush(stdin);
- }
- printf("\nNothing to do...");
- closesocket(clientSocket);
- printf("\nClient disconnected!\n\n");
- }
- closesocket(listeningSocket);
- printf("\nListening socket closed!\n\n");
- // Shutdown Winsock
- WSACleanup();
- return NETWORK_OK;
- }
- void taemot(clientSocket){
- char arr2[255];
- memset(arr2,0,sizeof(arr2));
- if(recv(clientSocket, arr2,sizeof(arr2),0) > 0)
- printf("\n%s \n", arr2);
- }
- void skicka(int clientSocket){
- char arr[255];
- if(kbhit())
- gets(arr);
- if(send(clientSocket, arr,sizeof(arr),0) > 0)
- fflush(stdin);
- memset(arr,0,sizeof(arr));
- }
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement