Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int i;
char ip[20];
char version[] = {"Senden"};
FILE *f;
WSADATA wsa;
int s;
int senden;
struct sockaddr_in addr;
char text[75];
char own_name[60];
char name[60];
LPCSTR lpszAppName = version;
LPCSTR lpszTitle = version;
HINSTANCE hInst;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd, bButton, hList;
MSG msg;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION));
wc.hbrBackground = GetStockObject(DKGRAY_BRUSH);
wc.lpszClassName = lpszAppName;
wc.lpszMenuName = NULL;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 16, 16, 0);
if( RegisterClassEx(&wc) == 0)
return 0;
hInst=hInstance;
hWnd = CreateWindowEx(NULL,
lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
200,
200,
400,
100,
NULL,
NULL,
hInstance,
NULL);
bButton = CreateWindow("BUTTON","Senden",WS_CHILD|WS_VISIBLE|
BS_DEFPUSHBUTTON, 140, 40, 100, 20,
hWnd, (HMENU)BUTTON, hInstance, NULL);
if( hWnd == NULL)
return 0;
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{
HWND hEdit;
switch (umsg)
{
case WM_CREATE:
{
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE ,
10, 10, 368, 20, hWnd, (HMENU)IDC_EDIT, GetModuleHandle(NULL), NULL);
SendMessage(hEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
break;
}
case WM_DESTROY:
remove("send");
PostQuitMessage(0);
return 0;
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case BUTTON:
memset(ip, 0, sizeof(ip));
//Socket wird vorbereitet
f = fopen("own_name", "r");
while( fgets(name, sizeof(name), f) !=0 )
{}
fclose(f);
strcpy(own_name, "_");
strcat(own_name, name);
f = fopen("send", "r");
fgets(ip, sizeof(ip), f);
fclose(f);
MessageBox(NULL,ip,"TEST",MB_OK);
addr.sin_family = AF_INET;
addr.sin_port = htons(7700);
addr.sin_addr.s_addr = inet_addr( ip );
MessageBox(NULL,ip,"TEST",MB_OK);
if (WSAStartup(MAKEWORD(1, 1), &wsa))
{
MessageBox(NULL,"WSAStartup() failed!","ERROR",MB_OK | MB_ICONERROR);
}
s = socket(AF_INET,SOCK_STREAM,0);
if (connect(s, (struct sockaddr*) &addr, sizeof(addr)) < 0)
{
MessageBox(NULL, "Fehler beim Senden!", "ERROR",MB_OK | MB_ICONERROR);
}
GetDlgItemText( hWnd, IDC_EDIT, text, sizeof(text) -15);
strcat(text, own_name);
senden = send(s, text, strlen(text), 0);
closesocket(s);
PostQuitMessage(0);
return 0;
break;
}
break;
}
return DefWindowProc(hWnd, umsg, wParam, lParam);
}
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <mmsystem.h>
#include <winsock.h>
int main(int argc, char *argv[])
{
WSADATA wsa;
char user_name[60];
int i = 0;
int j = 0;
int s;
int c;
int empfangen;
char text[75];
char message[75];
struct sockaddr_in cli;
int cli_size;
struct sockaddr_in addr;
FILE * f;
if (WSAStartup(MAKEWORD(1, 1), &wsa))
{
printf("WSAStartup() failed, %lu\n", (unsigned long)GetLastError());
return EXIT_FAILURE;
}
s = socket(AF_INET,SOCK_STREAM,0);
memset( &addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(7700);
addr.sin_addr.s_addr = htonl( INADDR_ANY );
if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) == -1)
{
perror("bind() failed");
}
if (listen(s, 3) == -1)
{
perror("listen () failed");
}
for(;;)
{
cli_size = sizeof(cli);
c = accept(s, (struct sockaddr*) &cli, &cli_size);
printf("Verbindung von %s\n", inet_ntoa(cli.sin_addr));
empfangen = recv(c, text, sizeof(text) - 1, 0);
PlaySound("neue_nachricht.wav",NULL,SND_FILENAME);
if (empfangen > 0)
{
text[empfangen] = '\0';
strcpy(message, text);
i = sizeof(message);
while ( message[i] != 95)
{
message[i] = 0;
i--;
}
message[i] = 0;
i = 0;
while( text[i] != 95)
i++;
i++;
while ( text[i] != 0)
{
user_name[j] = text[i];
j++;
i++;
}
strcat(user_name, " (");
strcat(user_name, inet_ntoa(cli.sin_addr));
strcat(user_name, ")");
f = fopen("usr", "w");
fputs(user_name, f);
fclose(f);
f = fopen("msg", "w");
fputs(message,f);
fclose(f);
i = 0;
while( user_name[i] != sizeof(user_name))
{
user_name[i] = 0;
i++;
}
i = 0;
j = 0;
}
else
{
printf("Fehler beim lesen\n");
}
closesocket(c);
}
closesocket(s);
getchar();
return 0;
}
I hope someone can help me.