// Module Name: Client.c
#include <winsock2.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5150
int g_port = DEFAULT_PORT; // Port on server to connect to
char g_szServerName[128] = "193.255.21.34"; //"KRANDAXP"; // Server to connect to
int main(void)
{
WSADATA wsd;
SOCKET sClient;
int ret, ch;
struct sockaddr_in server;
struct hostent *host = NULL;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
printf("Failed to load Winsock library!\n");
return 1;
}
//
// Create the socket, and attempt to connect to the server
//
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError());
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(g_port);
server.sin_addr.s_addr = inet_addr(g_szServerName);
//
// If the supplied server address wasn't in the form
// "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it
//
if (server.sin_addr.s_addr == INADDR_NONE) {
host = gethostbyname(g_szServerName);
if (host == NULL) {
printf("Unable to resolve server: %s\n", g_szServerName);
return 1;
}
CopyMemory(&server.sin_addr, host->h_addr_list[0], host->h_length);
}
if (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
printf("connect() failed: %d\n", WSAGetLastError());
return 1;
}
for (;;) {
ch = getch();
if (ch == '\r')
ch = '\n';
putchar(ch);
ret = send(sClient, (char *) &ch, 1, 0);
if (ret == 0)
break;
if (ret == SOCKET_ERROR) {
printf("send() failed: %d\n", WSAGetLastError());
break;
}
if (ch == '\x1b')
break;
}
closesocket(sClient);
WSACleanup();
return 0;
}
/**************************SERVER.C*******************************/
// Module Name: Server.c
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5150
int g_port = DEFAULT_PORT; // Port to listen for clients on
char g_szAddress[128]; // Interface to listen for clients on
DWORD WINAPI ClientThread(LPVOID lpParam)
{
SOCKET sock=(SOCKET)lpParam;
int ret;
char ch;
for (;;) {
ret = recv(sock, &ch, 1, 0);
if (ret == 0) // Graceful close
break;
if (ret == SOCKET_ERROR) {
printf("recv() failed: %d\n", WSAGetLastError());
break;
}
if (ch == '\x1b')
break;
putchar(ch);
}
return 0;
}
int main(void)
{
WSADATA wsd;
SOCKET sListen,sClient;
int addrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local, client;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
printf("Failed to load Winsock!\n");
return 1;
}
// Create our listening socket
//
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sListen == SOCKET_ERROR) {
printf("socket() failed: %d\n", WSAGetLastError());
return 1;
}
// Select the local interface and bind to it
//
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(g_port);
if (bind(sListen, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR) {
printf("bind() failed: %d\n", WSAGetLastError());
return 1;
}
listen(sListen, 8);
for (;;) {
addrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr *) &client, &addrSize);
if (sClient == INVALID_SOCKET) {
printf("accept() failed: %d\n", WSAGetLastError());
break;
}
printf("Accepted client: %s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)sClient, 0, &dwThreadId);
if (hThread == NULL) {
printf("CreateThread() failed: %d\n", GetLastError());
break;
}
CloseHandle(hThread);
}
closesocket(sListen);
WSACleanup();
return 0;
}
ayrıca proje kodları için : http://www.iucoders.com/attachments/soket_programlama.zip