-1

I keep getting errors in my compiler in brackets of code here {}

All the code works in the compiler except a list of bracket errors for my client and server.

I assume these block declarations give me syntax errors in this structure.

Or that link to source file is incompatible.

Or is there some other syntax reason for this error in the brackets?

error: expected identifier or '(' before '{' token

#define WIN32_LEAN_AND_MEAN
    
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <stdio.h>
#include "wclient.c"
    
#pragma comment(lib, "Ws2_32.lib")
    
int main() 
{
    int wsaserv;
    
    WSADATA serv;
    
    wsaserv = WSAStartup(MAKEWORD(2,2) , &serv);
    
    return 0;
}
    
int servsock;
    
{
    servsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
    
struct sockaddr_in servaddr;
    
{
    servaddr.sin_family = PF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(80)
    memset(servaddr, 0, sizeof(servaddr));
};
    
int wire;
    
{
    wire = listen(servsock, SOMAXCONN );
}
    
int srvb;
    
{
    srvb = bind(servsock, (struct sockaddr *)&servaddr, sizeof(servaddr));
}
    
int prxy;
    
{
    prxy = accept(servsock, (struct sockaddr *)&servaddr, sizeof(servaddr));
    for(;;)
    return 0;
}
    
int rs, sr;
    
{
    rs = send(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv), 0 );
    sr = recv(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv), 0 );
}

WSACleanup();

Here is the client side for linking:

#define WIN32_LEAN_AND_MEAN
    
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <stdio.h>
    
#pragma comment(lib, "Ws2_32.lib")
    
int wsacli;
    
{
    WSADATA cli;
    
    wsacli = WSAStartup (MAKEWORD(2,2) , &cli);
}
    
int clisock;
    
{
    clisock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
}
    
struct sockaddr_in cliserv;
    
{
    cliserv.sin_family = PF_INET;
    cliserv.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    cliserv.sin_port = htons(80);
    memset(cliserv, 0, sizeof(cliserv));
};
    
int clcnct;
    
{
    clcnct =  connect(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv));
}
    
int clcnct;
    
{
    clibnd = bind(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv));
}
    
int sc, rc;
    
{
    sc = send(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv), 0 );
    rc = recv(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv), 0 );
}
    
WSACleanup();

Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\matt\Documents\Code-softwares> gcc wserver.c wclient.c lws2_32
In file included from wserver.c:8:
wclient.c:18:1: error: expected identifier or '(' before '{' token
   18 | {
      | ^
wclient.c:36:1: error: expected identifier or '(' before '{' token
   36 | {
      | ^
wclient.c:47:1: error: expected identifier or '(' before '{' token
   47 | {
      | ^
wclient.c:64:1: error: expected identifier or '(' before '{' token
   64 | {
      | ^
wclient.c:76:1: error: expected identifier or '(' before '{' token
   76 | {
      | ^
wclient.c:88:1: error: expected identifier or '(' before '{' token
   88 | {
      | ^
wclient.c:99:2: warning: data definition has no type or storage class
   99 |  WSACleanup();
      |  ^~~~~~~~~~
wclient.c:99:2: warning: type defaults to 'int' in declaration of 'WSACleanup' [-Wimplicit-int]
wclient.c:99:2: warning: 'WSACleanup' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
wserver.c:17:1: error: expected identifier or '(' before '{' token
   17 | {
      | ^
wserver.c:69:1: error: expected identifier or '(' before '{' token
   69 | {
      | ^
wserver.c:80:1: error: expected identifier or '(' before '{' token
   80 | {
      | ^
wserver.c:93:1: error: expected identifier or '(' before '{' token
   93 | {
      | ^
wserver.c:102:1: error: expected identifier or '(' before '{' token
  102 | {
      | ^
wserver.c:111:1: error: expected identifier or '(' before '{' token
  111 | {
      | ^
wserver.c:136:1: error: expected identifier or '(' before '{' token
  136 | {
      | ^
wserver.c:150:2: warning: data definition has no type or storage class
  150 |  WSACleanup();
      |  ^~~~~~~~~~
wserver.c:150:2: warning: type defaults to 'int' in declaration of 'WSACleanup' [-Wimplicit-int]
wclient.c:18:1: error: expected identifier or '(' before '{' token
   18 | {
      | ^
wclient.c:36:1: error: expected identifier or '(' before '{' token
   36 | {
      | ^
wclient.c:47:1: error: expected identifier or '(' before '{' token
   47 | {
      | ^
wclient.c:64:1: error: expected identifier or '(' before '{' token
   64 | {
      | ^
wclient.c:76:1: error: expected identifier or '(' before '{' token
   76 | {
      | ^
wclient.c:88:1: error: expected identifier or '(' before '{' token
   88 | {
      | ^
wclient.c:99:2: warning: data definition has no type or storage class
   99 |  WSACleanup();
      |  ^~~~~~~~~~
wclient.c:99:2: warning: type defaults to 'int' in declaration of 'WSACleanup' [-Wimplicit-int]
wclient.c:99:2: warning: 'WSACleanup' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
PS C:\Users\matt\Documents\Code-softwares>
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • if i declare them and initialize them int datatype = functionalcode(interface function valuies) . Can i do without brackets. Or can i remove brackets and get same functionality – matthew radon May 11 '22 at 19:32
  • 2
    Clearly, you don't understand how C programs are structured, so I suggest you stop what you are doing and go back to basics. Get yourself some [good C books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Remy Lebeau May 11 '22 at 19:39
  • If you can't fix syntax errors, you are not ready for socket programming. I have no clue how you ended up like this. – Cheatah May 11 '22 at 19:48

1 Answers1

0

You can't have blocks of executable code in global scope outside of functions. Have you never worked with a main() function before? Your server code has one, but is not using it correctly. Your client code doesn't even have one.

Try something more like this instead:

Server:

#define WIN32_LEAN_AND_MEAN
    
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <stdio.h>
#include "wclient.c"

#pragma comment(lib, "Ws2_32.lib")

int main() 
{
    WSADATA serv;
    
    int wsaserv = WSAStartup(MAKEWORD(2,2), &serv);
    if (wsaserv != 0)
    {
        printf("WSAStartup error: %d\n", wsaserv);
        return 0;
    }
    
    SOCKET servsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (servsock == INVALID_SOCKET)
    {
        printf("socket error: %d\n", WSAGetLastError());
        WSACleanup();
        return 0;
    }

    struct sockaddr_in servaddr;
    memset(servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(80);
    
    int srvb = bind(servsock, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if (srvb == SOCKET_ERROR)
    {
        printf("bind error: %d\n", WSAGetLastError());
        closesocket(servsock);
        WSACleanup();
        return 0;
    }
    
    int wire = listen(servsock, SOMAXCONN);
    if (wire == SOCKET_ERROR)
    {
        printf("listen error: %d\n", WSAGetLastError());
        closesocket(servsock);
        WSACleanup();
        return 0;
    }

    for(;;)
    {
        int addrlen = sizeof(servaddr);
        SOCKET prxy = accept(servsock, (struct sockaddr *)&servaddr, &addrlen);
        if (prxy != INVALID_SOCKET)
        {
            // send()/recv() with prxy as needed...
            closesocket(prxy);
        }
    }

    closesocket(servsock);
    WSACleanup();

    return 0;
}    

Client:

#define WIN32_LEAN_AND_MEAN
    
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <stdio.h>
    
#pragma comment(lib, "Ws2_32.lib")
    
int main()
{
    WSADATA cli;
    
    int wsacli = WSAStartup (MAKEWORD(2,2), &cli);
    if (wsacli != 0)
    {
        printf("WSAStartup error: %d\n", wsacli);
        return 0;
    }

    SOCKET clisock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (clisock == INVALID_SOCKET)
    {
        printf("socket error: %d\n", WSAGetLastError());
        WSACleanup();
        return 0;
    }
    
    struct sockaddr_in cliserv;
    memset(cliserv, 0, sizeof(cliserv));
    cliserv.sin_family = AF_INET;
    cliserv.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    cliserv.sin_port = htons(80);

    int clcnct = connect(clisock, (struct sockaddr *)&cliserv, sizeof(cliserv));
    if (clcnct == SOCKET_ERROR)
    {
        printf("connect error: %d\n", WSAGetLastError());
        closesocket(clisock);
        WSACleanup();
        return 0;
    }

    // send()/recv() with clisock as needed ...

    closesocket(clisock);
    WSACleanup();

    return 0;
}    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770